source: mainline/uspace/drv/bus/usb/xhci/endpoint.c@ abb5d08

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since abb5d08 was 682c9354, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

xhci: move all real functionality from bus to device/endpoint/transfers

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