| 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 | int xhci_endpoint_init(xhci_endpoint_t *xhci_ep, device_t *dev, const usb_endpoint_desc_t *desc)
|
|---|
| 48 | {
|
|---|
| 49 | assert(xhci_ep);
|
|---|
| 50 |
|
|---|
| 51 | endpoint_t *ep = &xhci_ep->base;
|
|---|
| 52 |
|
|---|
| 53 | endpoint_init(ep, dev, desc);
|
|---|
| 54 |
|
|---|
| 55 | xhci_ep->max_streams = desc->usb3.max_streams;
|
|---|
| 56 | xhci_ep->max_burst = desc->usb3.max_burst;
|
|---|
| 57 | xhci_ep->mult = desc->usb3.mult;
|
|---|
| 58 |
|
|---|
| 59 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 60 | xhci_ep->isoch_max_size = desc->usb3.bytes_per_interval
|
|---|
| 61 | ? desc->usb3.bytes_per_interval
|
|---|
| 62 | : desc->max_packet_size * (desc->packets + 1);
|
|---|
| 63 | /* Technically there could be superspeed plus too. */
|
|---|
| 64 |
|
|---|
| 65 | /* Allocate and setup isochronous-specific structures. */
|
|---|
| 66 | xhci_ep->isoch_enqueue = 0;
|
|---|
| 67 | xhci_ep->isoch_dequeue = XHCI_ISOCH_BUFFER_COUNT - 1;
|
|---|
| 68 | xhci_ep->isoch_started = false;
|
|---|
| 69 |
|
|---|
| 70 | fibril_mutex_initialize(&xhci_ep->isoch_guard);
|
|---|
| 71 | fibril_condvar_initialize(&xhci_ep->isoch_avail);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return EOK;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
|
|---|
| 78 | {
|
|---|
| 79 | assert(xhci_ep);
|
|---|
| 80 |
|
|---|
| 81 | // TODO: Something missed?
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static int xhci_endpoint_type(xhci_endpoint_t *ep)
|
|---|
| 85 | {
|
|---|
| 86 | const bool in = ep->base.direction == USB_DIRECTION_IN;
|
|---|
| 87 |
|
|---|
| 88 | switch (ep->base.transfer_type) {
|
|---|
| 89 | case USB_TRANSFER_CONTROL:
|
|---|
| 90 | return EP_TYPE_CONTROL;
|
|---|
| 91 |
|
|---|
| 92 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 93 | return in ? EP_TYPE_ISOCH_IN
|
|---|
| 94 | : EP_TYPE_ISOCH_OUT;
|
|---|
| 95 |
|
|---|
| 96 | case USB_TRANSFER_BULK:
|
|---|
| 97 | return in ? EP_TYPE_BULK_IN
|
|---|
| 98 | : EP_TYPE_BULK_OUT;
|
|---|
| 99 |
|
|---|
| 100 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 101 | return in ? EP_TYPE_INTERRUPT_IN
|
|---|
| 102 | : EP_TYPE_INTERRUPT_OUT;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return EP_TYPE_INVALID;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | static bool endpoint_using_streams(xhci_endpoint_t *xhci_ep)
|
|---|
| 109 | {
|
|---|
| 110 | return xhci_ep->primary_stream_ctx_array != NULL;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | static size_t primary_stream_ctx_array_max_size(xhci_endpoint_t *xhci_ep)
|
|---|
| 114 | {
|
|---|
| 115 | if (!xhci_ep->max_streams)
|
|---|
| 116 | return 0;
|
|---|
| 117 |
|
|---|
| 118 | /* Section 6.2.3, Table 61 */
|
|---|
| 119 | return 1 << (xhci_ep->max_streams + 1);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // static bool primary_stream_ctx_has_secondary_array(xhci_stream_ctx_t *primary_ctx) {
|
|---|
| 123 | // /* Section 6.2.4.1, SCT values */
|
|---|
| 124 | // return XHCI_STREAM_SCT(*primary_ctx) >= 2;
|
|---|
| 125 | // }
|
|---|
| 126 | //
|
|---|
| 127 | // static size_t secondary_stream_ctx_array_size(xhci_stream_ctx_t *primary_ctx) {
|
|---|
| 128 | // if (XHCI_STREAM_SCT(*primary_ctx) < 2) return 0;
|
|---|
| 129 | // return 2 << XHCI_STREAM_SCT(*primary_ctx);
|
|---|
| 130 | // }
|
|---|
| 131 |
|
|---|
| 132 | static void initialize_primary_streams(xhci_hc_t *hc, xhci_endpoint_t *xhci_ep, unsigned count) {
|
|---|
| 133 | for (size_t index = 0; index < count; ++index) {
|
|---|
| 134 | xhci_stream_ctx_t *ctx = &xhci_ep->primary_stream_ctx_array[index];
|
|---|
| 135 | xhci_trb_ring_t *ring = &xhci_ep->primary_stream_rings[index];
|
|---|
| 136 |
|
|---|
| 137 | /* Init and register TRB ring for every primary stream */
|
|---|
| 138 | xhci_trb_ring_init(ring);
|
|---|
| 139 | XHCI_STREAM_DEQ_PTR_SET(*ctx, ring->dequeue);
|
|---|
| 140 |
|
|---|
| 141 | /* Set to linear stream array */
|
|---|
| 142 | XHCI_STREAM_SCT_SET(*ctx, 1);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static void setup_stream_context(xhci_endpoint_t *xhci_ep, xhci_ep_ctx_t *ctx, unsigned pstreams) {
|
|---|
| 147 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(xhci_ep));
|
|---|
| 148 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, xhci_ep->base.max_packet_size);
|
|---|
| 149 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, xhci_ep->max_burst);
|
|---|
| 150 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 151 |
|
|---|
| 152 | XHCI_EP_MAX_P_STREAMS_SET(*ctx, pstreams);
|
|---|
| 153 | XHCI_EP_TR_DPTR_SET(*ctx, xhci_ep->primary_stream_ctx_dma.phys);
|
|---|
| 154 | // TODO: set HID?
|
|---|
| 155 | XHCI_EP_LSA_SET(*ctx, 1);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int xhci_endpoint_request_streams(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *xhci_ep, unsigned count) {
|
|---|
| 159 | if (xhci_ep->base.transfer_type != USB_TRANSFER_BULK
|
|---|
| 160 | || dev->base.speed != USB_SPEED_SUPER) {
|
|---|
| 161 | usb_log_error("Streams are only supported by superspeed bulk endpoints.");
|
|---|
| 162 | return EINVAL;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if (!primary_stream_ctx_array_max_size(xhci_ep)) {
|
|---|
| 166 | usb_log_error("Streams are not supported by endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 167 | return EINVAL;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | uint8_t max_psa_size = 2 << XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PSA_SIZE);
|
|---|
| 171 | if (count > max_psa_size) {
|
|---|
| 172 | // FIXME: We don't support secondary stream arrays yet, so we just give up for this
|
|---|
| 173 | return ENOTSUP;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (count > (unsigned) (1 << xhci_ep->max_streams)) {
|
|---|
| 177 | usb_log_error("Endpoint " XHCI_EP_FMT " supports only %u streams.",
|
|---|
| 178 | XHCI_EP_ARGS(*xhci_ep), (1 << xhci_ep->max_streams));
|
|---|
| 179 | return EINVAL;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if (count <= 1024) {
|
|---|
| 183 | usb_log_debug2("Allocating primary stream context array of size %u for endpoint " XHCI_EP_FMT,
|
|---|
| 184 | count, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 185 | if ((dma_buffer_alloc(&xhci_ep->primary_stream_ctx_dma, count * sizeof(xhci_stream_ctx_t))))
|
|---|
| 186 | return ENOMEM;
|
|---|
| 187 | xhci_ep->primary_stream_ctx_array = xhci_ep->primary_stream_ctx_dma.virt;
|
|---|
| 188 |
|
|---|
| 189 | xhci_ep->primary_stream_rings = calloc(count, sizeof(xhci_trb_ring_t));
|
|---|
| 190 | if (!xhci_ep->primary_stream_rings) {
|
|---|
| 191 | dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
|
|---|
| 192 | return ENOMEM;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // FIXME: count should be rounded to nearest power of 2 for xHC, workaround for now
|
|---|
| 196 | count = 1024;
|
|---|
| 197 | // FIXME: pstreams are "log2(count) - 1"
|
|---|
| 198 | const size_t pstreams = 9;
|
|---|
| 199 | xhci_ep->primary_stream_ctx_array_size = count;
|
|---|
| 200 |
|
|---|
| 201 | memset(xhci_ep->primary_stream_ctx_array, 0, count * sizeof(xhci_stream_ctx_t));
|
|---|
| 202 | initialize_primary_streams(hc, xhci_ep, count);
|
|---|
| 203 |
|
|---|
| 204 | xhci_ep_ctx_t ep_ctx;
|
|---|
| 205 | setup_stream_context(xhci_ep, &ep_ctx, pstreams);
|
|---|
| 206 | return hc_add_endpoint(hc, dev->slot_id, xhci_endpoint_index(xhci_ep), &ep_ctx);
|
|---|
| 207 | }
|
|---|
| 208 | // FIXME: Complex stuff not supported yet
|
|---|
| 209 | return ENOTSUP;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | static int xhci_isoch_alloc_transfers(xhci_endpoint_t *xhci_ep) {
|
|---|
| 213 | int i = 0;
|
|---|
| 214 | int err = EOK;
|
|---|
| 215 | while (i < XHCI_ISOCH_BUFFER_COUNT) {
|
|---|
| 216 | xhci_isoch_transfer_t *transfer = &xhci_ep->isoch_transfers[i];
|
|---|
| 217 | if (dma_buffer_alloc(&transfer->data, xhci_ep->isoch_max_size)) {
|
|---|
| 218 | err = ENOMEM;
|
|---|
| 219 | break;
|
|---|
| 220 | }
|
|---|
| 221 | transfer->size = 0;
|
|---|
| 222 | ++i;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (err) {
|
|---|
| 226 | --i;
|
|---|
| 227 | while(i >= 0) {
|
|---|
| 228 | dma_buffer_free(&xhci_ep->isoch_transfers[i].data);
|
|---|
| 229 | --i;
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | return err;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | int xhci_endpoint_alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| 237 | {
|
|---|
| 238 | /* Can't use XHCI_EP_FMT because the endpoint may not have device. */
|
|---|
| 239 | usb_log_debug2("Allocating main transfer ring for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 240 |
|
|---|
| 241 | xhci_ep->primary_stream_ctx_array = NULL;
|
|---|
| 242 |
|
|---|
| 243 | int err;
|
|---|
| 244 | if ((err = xhci_trb_ring_init(&xhci_ep->ring))) {
|
|---|
| 245 | return err;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 249 | if ((err = xhci_isoch_alloc_transfers(xhci_ep))) {
|
|---|
| 250 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| 251 | return err;
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | return EOK;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | void xhci_endpoint_free_transfer_ds(xhci_endpoint_t *xhci_ep)
|
|---|
| 259 | {
|
|---|
| 260 | if (endpoint_using_streams(xhci_ep)) {
|
|---|
| 261 | usb_log_debug2("Freeing primary stream context array of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 262 |
|
|---|
| 263 | // maybe check if LSA, then skip?
|
|---|
| 264 | // for (size_t index = 0; index < primary_stream_ctx_array_size(xhci_ep); ++index) {
|
|---|
| 265 | // xhci_stream_ctx_t *primary_ctx = xhci_ep->primary_stream_ctx_array + index;
|
|---|
| 266 | // if (primary_stream_ctx_has_secondary_array(primary_ctx)) {
|
|---|
| 267 | // // uintptr_t phys = XHCI_STREAM_DEQ_PTR(*primary_ctx);
|
|---|
| 268 | // /* size_t size = */ secondary_stream_ctx_array_size(primary_ctx);
|
|---|
| 269 | // // TODO: somehow map the address to virtual and free the secondary array
|
|---|
| 270 | // }
|
|---|
| 271 | // }
|
|---|
| 272 | for (size_t index = 0; index < xhci_ep->primary_stream_ctx_array_size; ++index) {
|
|---|
| 273 | // FIXME: Get the trb ring associated with stream [index] and fini it
|
|---|
| 274 | }
|
|---|
| 275 | dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
|
|---|
| 276 | } else {
|
|---|
| 277 | usb_log_debug2("Freeing main transfer ring of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
|
|---|
| 278 |
|
|---|
| 279 | xhci_trb_ring_fini(&xhci_ep->ring);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
|---|
| 283 | for (size_t i = 0; i < XHCI_ISOCH_BUFFER_COUNT; ++i) {
|
|---|
| 284 | dma_buffer_free(&xhci_ep->isoch_transfers[i].data);
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /** See section 4.5.1 of the xHCI spec.
|
|---|
| 290 | */
|
|---|
| 291 | uint8_t xhci_endpoint_dci(xhci_endpoint_t *ep)
|
|---|
| 292 | {
|
|---|
| 293 | return (2 * ep->base.endpoint) +
|
|---|
| 294 | (ep->base.transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 295 | || ep->base.direction == USB_DIRECTION_IN);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /** Return an index to the endpoint array. The indices are assigned as follows:
|
|---|
| 299 | * 0 EP0 BOTH
|
|---|
| 300 | * 1 EP1 OUT
|
|---|
| 301 | * 2 EP1 IN
|
|---|
| 302 | *
|
|---|
| 303 | * For control endpoints >0, the IN endpoint index is used.
|
|---|
| 304 | *
|
|---|
| 305 | * The index returned must be usually offset by a number of contexts preceding
|
|---|
| 306 | * the endpoint contexts themselves.
|
|---|
| 307 | */
|
|---|
| 308 | uint8_t xhci_endpoint_index(xhci_endpoint_t *ep)
|
|---|
| 309 | {
|
|---|
| 310 | return xhci_endpoint_dci(ep) - 1;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 314 | {
|
|---|
| 315 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 316 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| 317 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
|
|---|
| 318 | XHCI_EP_MULT_SET(*ctx, ep->mult);
|
|---|
| 319 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 320 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 321 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 325 | {
|
|---|
| 326 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 327 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
|
|---|
| 328 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
|
|---|
| 329 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 330 |
|
|---|
| 331 | XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
|
|---|
| 332 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 333 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 337 | {
|
|---|
| 338 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 339 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| 340 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
|
|---|
| 341 | XHCI_EP_MULT_SET(*ctx, ep->mult);
|
|---|
| 342 | XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
|
|---|
| 343 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 344 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 345 |
|
|---|
| 346 | XHCI_EP_MAX_ESIT_PAYLOAD_LO_SET(*ctx, ep->isoch_max_size & 0xFFFF);
|
|---|
| 347 | XHCI_EP_MAX_ESIT_PAYLOAD_HI_SET(*ctx, (ep->isoch_max_size >> 16) & 0xFF);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
|
|---|
| 351 | {
|
|---|
| 352 | XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
|
|---|
| 353 | XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
|
|---|
| 354 | XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
|
|---|
| 355 | XHCI_EP_MULT_SET(*ctx, 0);
|
|---|
| 356 | XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
|
|---|
| 357 | XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
|
|---|
| 358 | XHCI_EP_DCS_SET(*ctx, 1);
|
|---|
| 359 | // TODO: max ESIT payload
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
|
|---|
| 363 |
|
|---|
| 364 | static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
|
|---|
| 365 | [USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
|
|---|
| 366 | [USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
|
|---|
| 367 | [USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
|
|---|
| 368 | [USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
|
|---|
| 369 | };
|
|---|
| 370 |
|
|---|
| 371 | void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
|
|---|
| 372 | {
|
|---|
| 373 | assert(ep);
|
|---|
| 374 | assert(ep_ctx);
|
|---|
| 375 |
|
|---|
| 376 | usb_transfer_type_t tt = ep->base.transfer_type;
|
|---|
| 377 | assert(tt < ARRAY_SIZE(setup_ep_ctx_helpers));
|
|---|
| 378 |
|
|---|
| 379 | memset(ep_ctx, 0, sizeof(*ep_ctx));
|
|---|
| 380 | setup_ep_ctx_helpers[tt](ep, ep_ctx);
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | int xhci_device_add_endpoint(xhci_device_t *dev, xhci_endpoint_t *ep)
|
|---|
| 384 | {
|
|---|
| 385 | assert(dev);
|
|---|
| 386 | assert(ep);
|
|---|
| 387 |
|
|---|
| 388 | /* Offline devices don't create new endpoints other than EP0. */
|
|---|
| 389 | if (!dev->online && ep->base.endpoint > 0) {
|
|---|
| 390 | return EAGAIN;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | const usb_endpoint_t ep_num = ep->base.endpoint;
|
|---|
| 394 |
|
|---|
| 395 | if (dev->endpoints[ep_num])
|
|---|
| 396 | return EEXIST;
|
|---|
| 397 |
|
|---|
| 398 | /* Device reference */
|
|---|
| 399 | endpoint_add_ref(&ep->base);
|
|---|
| 400 | ep->base.device = &dev->base;
|
|---|
| 401 | dev->endpoints[ep_num] = ep;
|
|---|
| 402 |
|
|---|
| 403 | return EOK;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | void xhci_device_remove_endpoint(xhci_endpoint_t *ep)
|
|---|
| 407 | {
|
|---|
| 408 | assert(ep);
|
|---|
| 409 | xhci_device_t *dev = xhci_device_get(ep->base.device);
|
|---|
| 410 |
|
|---|
| 411 | assert(dev->endpoints[ep->base.endpoint]);
|
|---|
| 412 | dev->endpoints[ep->base.endpoint] = NULL;
|
|---|
| 413 | ep->base.device = NULL;
|
|---|
| 414 |
|
|---|
| 415 | endpoint_del_ref(&ep->base);
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | xhci_endpoint_t *xhci_device_get_endpoint(xhci_device_t *dev, usb_endpoint_t ep)
|
|---|
| 419 | {
|
|---|
| 420 | return dev->endpoints[ep];
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | /**
|
|---|
| 424 | * @}
|
|---|
| 425 | */
|
|---|