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