HelenOS sources
This source file includes following definitions.
- xhci_endpoint_init
- xhci_endpoint_create
- xhci_endpoint_fini
- xhci_endpoint_destroy
- xhci_endpoint_register
- endpoint_abort
- xhci_endpoint_unregister
- xhci_endpoint_type
- alloc_transfer_ds
- xhci_endpoint_free_transfer_ds
- xhci_endpoint_get_ring
- setup_control_ep_ctx
- setup_bulk_ep_ctx
- setup_isoch_ep_ctx
- setup_interrupt_ep_ctx
- xhci_setup_endpoint_context
- xhci_endpoint_clear_halt
#include <usb/host/endpoint.h>
#include <usb/descriptor.h>
#include <errno.h>
#include <macros.h>
#include <str_error.h>
#include "hc.h"
#include "bus.h"
#include "commands.h"
#include "device.h"
#include "endpoint.h"
#include "streams.h"
static errno_t alloc_transfer_ds(xhci_endpoint_t *);
static errno_t xhci_endpoint_init(xhci_endpoint_t *xhci_ep, device_t *dev,
const usb_endpoint_descriptors_t *desc)
{
errno_t rc;
assert(xhci_ep);
endpoint_t *ep = &xhci_ep->base;
endpoint_init(ep, dev, desc);
fibril_mutex_initialize(&xhci_ep->guard);
xhci_ep->max_burst = desc->companion.max_burst + 1;
if (ep->transfer_type == USB_TRANSFER_BULK)
xhci_ep->max_streams = 1 << (USB_SSC_MAX_STREAMS(desc->companion));
else
xhci_ep->max_streams = 1;
if (ep->transfer_type == USB_TRANSFER_ISOCHRONOUS)
xhci_ep->mult = USB_SSC_MULT(desc->companion) + 1;
else
xhci_ep->mult = 1;
if (dev->speed >= USB_SPEED_SUPER) {
ep->packets_per_uframe = xhci_ep->max_burst * xhci_ep->mult;
if (ep->transfer_type == USB_TRANSFER_ISOCHRONOUS ||
ep->transfer_type == USB_TRANSFER_INTERRUPT) {
ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
}
}
xhci_ep->interval = desc->endpoint.poll_interval;
if (dev->speed >= USB_SPEED_HIGH ||
ep->transfer_type != USB_TRANSFER_INTERRUPT) {
if (xhci_ep->interval == 0)
xhci_ep->interval = 8;
xhci_ep->interval = 1 << (xhci_ep->interval - 1);
}
if (dev->speed <= USB_SPEED_FULL) {
xhci_ep->interval *= 8;
}
if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS)
isoch_init(xhci_ep, desc);
if ((rc = alloc_transfer_ds(xhci_ep)))
goto err;
unsigned flags = -1U;
xhci_bus_t *bus = bus_to_xhci_bus(ep->device->bus);
if (bus->hc->ac64)
flags &= ~DMA_POLICY_4GiB;
ep->transfer_buffer_policy = dma_policy_create(flags, 1 << 16);
ep->required_transfer_buffer_policy = dma_policy_create(flags, PAGE_SIZE);
return EOK;
err:
return rc;
}
endpoint_t *xhci_endpoint_create(device_t *dev,
const usb_endpoint_descriptors_t *desc)
{
const usb_transfer_type_t type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t) +
(type == USB_TRANSFER_ISOCHRONOUS) * sizeof(*ep->isoch));
if (!ep)
return NULL;
if (xhci_endpoint_init(ep, dev, desc)) {
free(ep);
return NULL;
}
return &ep->base;
}
static void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
{
assert(xhci_ep);
xhci_endpoint_free_transfer_ds(xhci_ep);
}
void xhci_endpoint_destroy(endpoint_t *ep)
{
xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
xhci_endpoint_fini(xhci_ep);
free(xhci_ep);
}
errno_t xhci_endpoint_register(endpoint_t *ep_base)
{
errno_t err;
xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
if (ep_base->endpoint != 0 && (err = hc_add_endpoint(ep)))
return err;
endpoint_set_online(ep_base, &ep->guard);
return EOK;
}
static void endpoint_abort(endpoint_t *ep)
{
xhci_device_t *dev = xhci_device_get(ep->device);
xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
assert(xhci_ep->primary_stream_data_array == NULL);
fibril_mutex_lock(&xhci_ep->guard);
endpoint_set_offline_locked(ep);
if (!ep->active_batch) {
fibril_mutex_unlock(&xhci_ep->guard);
return;
}
endpoint_wait_timeout_locked(ep, 10000);
if (!ep->active_batch) {
fibril_mutex_unlock(&xhci_ep->guard);
return;
}
usb_transfer_batch_t *const batch = ep->active_batch;
const errno_t err = hc_stop_endpoint(xhci_ep);
if (err) {
usb_log_error("Failed to stop endpoint %u of device "
XHCI_DEV_FMT ": %s", ep->endpoint, XHCI_DEV_ARGS(*dev),
str_error(err));
}
fibril_mutex_unlock(&xhci_ep->guard);
batch->error = EINTR;
batch->transferred_size = 0;
usb_transfer_batch_finish(batch);
return;
}
void xhci_endpoint_unregister(endpoint_t *ep_base)
{
errno_t err;
xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
xhci_device_t *dev = xhci_device_get(ep_base->device);
endpoint_abort(ep_base);
if (ep_base->endpoint != 0 && dev->slot_id) {
if ((err = hc_drop_endpoint(ep))) {
usb_log_error("Failed to drop endpoint " XHCI_EP_FMT ": %s",
XHCI_EP_ARGS(*ep), str_error(err));
}
} else {
usb_log_debug("Not going to drop endpoint " XHCI_EP_FMT " because"
" the slot has already been disabled.", XHCI_EP_ARGS(*ep));
}
}
int xhci_endpoint_type(xhci_endpoint_t *ep)
{
const bool in = ep->base.direction == USB_DIRECTION_IN;
switch (ep->base.transfer_type) {
case USB_TRANSFER_CONTROL:
return EP_TYPE_CONTROL;
case USB_TRANSFER_ISOCHRONOUS:
return in ? EP_TYPE_ISOCH_IN :
EP_TYPE_ISOCH_OUT;
case USB_TRANSFER_BULK:
return in ? EP_TYPE_BULK_IN :
EP_TYPE_BULK_OUT;
case USB_TRANSFER_INTERRUPT:
return in ? EP_TYPE_INTERRUPT_IN :
EP_TYPE_INTERRUPT_OUT;
}
return EP_TYPE_INVALID;
}
static errno_t alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
{
usb_log_debug("Allocating main transfer ring for endpoint " XHCI_EP_FMT,
XHCI_EP_ARGS(*xhci_ep));
xhci_ep->primary_stream_data_array = NULL;
xhci_ep->primary_stream_data_size = 0;
errno_t err;
if ((err = xhci_trb_ring_init(&xhci_ep->ring, 0))) {
return err;
}
if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
if ((err = isoch_alloc_transfers(xhci_ep))) {
xhci_trb_ring_fini(&xhci_ep->ring);
return err;
}
}
return EOK;
}
void xhci_endpoint_free_transfer_ds(xhci_endpoint_t *xhci_ep)
{
if (xhci_ep->primary_stream_data_size) {
xhci_stream_free_ds(xhci_ep);
} else {
usb_log_debug("Freeing main transfer ring of endpoint " XHCI_EP_FMT,
XHCI_EP_ARGS(*xhci_ep));
xhci_trb_ring_fini(&xhci_ep->ring);
}
if (xhci_ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS)
isoch_fini(xhci_ep);
}
xhci_trb_ring_t *xhci_endpoint_get_ring(xhci_endpoint_t *ep, uint32_t stream_id)
{
if (ep->primary_stream_data_size == 0)
return stream_id == 0 ? &ep->ring : NULL;
xhci_stream_data_t *stream_data = xhci_get_stream_ctx_data(ep, stream_id);
if (stream_data == NULL) {
usb_log_warning("No transfer ring was found for stream %u.", stream_id);
return NULL;
}
return &stream_data->ring;
}
static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
{
XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
XHCI_EP_DCS_SET(*ctx, 1);
}
static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
{
XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
XHCI_EP_DCS_SET(*ctx, 1);
}
static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
{
XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
XHCI_EP_MULT_SET(*ctx, ep->mult - 1);
XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
XHCI_EP_DCS_SET(*ctx, 1);
XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32);
XHCI_EP_MAX_ESIT_PAYLOAD_LO_SET(*ctx, ep->isoch->max_size & 0xFFFF);
XHCI_EP_MAX_ESIT_PAYLOAD_HI_SET(*ctx, (ep->isoch->max_size >> 16) & 0xFF);
}
static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
{
XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst - 1);
XHCI_EP_MULT_SET(*ctx, 0);
XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
XHCI_EP_DCS_SET(*ctx, 1);
XHCI_EP_INTERVAL_SET(*ctx, fnzb32(ep->interval) % 32);
}
typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
[USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
[USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
[USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
[USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
};
void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
{
assert(ep);
assert(ep_ctx);
usb_transfer_type_t tt = ep->base.transfer_type;
memset(ep_ctx, 0, sizeof(*ep_ctx));
setup_ep_ctx_helpers[tt](ep, ep_ctx);
}
errno_t xhci_endpoint_clear_halt(xhci_endpoint_t *ep, uint32_t stream_id)
{
errno_t err;
if ((err = hc_reset_endpoint(ep)))
return err;
if ((err = hc_reset_ring(ep, stream_id)))
return err;
return EOK;
}
HelenOS homepage, sources at GitHub