HelenOS sources
This source file includes following definitions.
- ehci_transfer_batch_destroy
- ehci_transfer_batch_create
- ehci_transfer_batch_prepare
- ehci_transfer_batch_check_completed
- ehci_transfer_batch_commit
- batch_control
- batch_data
#include <assert.h>
#include <errno.h>
#include <macros.h>
#include <mem.h>
#include <stdbool.h>
#include <str_error.h>
#include <usb/usb.h>
#include <usb/debug.h>
#include "ehci_batch.h"
#include "ehci_bus.h"
#define EHCI_TD_MAX_TRANSFER (16 * 1024)
static void (*const batch_setup[])(ehci_transfer_batch_t *);
void ehci_transfer_batch_destroy(ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
dma_buffer_free(&ehci_batch->ehci_dma_buffer);
usb_log_debug2("Batch(%p): disposed", ehci_batch);
free(ehci_batch);
}
ehci_transfer_batch_t *ehci_transfer_batch_create(endpoint_t *ep)
{
assert(ep);
ehci_transfer_batch_t *ehci_batch = calloc(1, sizeof(ehci_transfer_batch_t));
if (!ehci_batch) {
usb_log_error("Failed to allocate EHCI batch data.");
return NULL;
}
usb_transfer_batch_init(&ehci_batch->base, ep);
return ehci_batch;
}
int ehci_transfer_batch_prepare(ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
const size_t setup_size = (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL) ?
USB_SETUP_PACKET_SIZE :
0;
const size_t size = ehci_batch->base.size;
ehci_batch->qh = ehci_endpoint_get(ehci_batch->base.ep)->qh;
ehci_batch->td_count = (size + EHCI_TD_MAX_TRANSFER - 1) /
EHCI_TD_MAX_TRANSFER;
if (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL) {
ehci_batch->td_count += 2;
}
assert(ehci_batch->td_count > 0);
const size_t tds_size = ehci_batch->td_count * sizeof(td_t);
if (dma_buffer_alloc(&ehci_batch->ehci_dma_buffer, tds_size + setup_size)) {
usb_log_error("Batch %p: Failed to allocate device buffer",
ehci_batch);
return ENOMEM;
}
ehci_batch->tds = ehci_batch->ehci_dma_buffer.virt;
memset(ehci_batch->tds, 0, tds_size);
ehci_batch->setup_buffer = ehci_batch->ehci_dma_buffer.virt + tds_size;
memcpy(ehci_batch->setup_buffer, ehci_batch->base.setup.buffer, setup_size);
ehci_batch->data_buffer = ehci_batch->base.dma_buffer.virt;
if (!batch_setup[ehci_batch->base.ep->transfer_type])
return ENOTSUP;
batch_setup[ehci_batch->base.ep->transfer_type](ehci_batch);
usb_log_debug("Batch %p %s " USB_TRANSFER_BATCH_FMT " initialized.",
ehci_batch, usb_str_direction(ehci_batch->base.dir),
USB_TRANSFER_BATCH_ARGS(ehci_batch->base));
return EOK;
}
bool ehci_transfer_batch_check_completed(ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
usb_log_debug("Batch %p: checking %zu td(s) for completion.",
ehci_batch, ehci_batch->td_count);
usb_log_debug2("Batch %p: QH: %08x:%08x:%08x:%08x:%08x:%08x.",
ehci_batch,
ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
ehci_batch->qh->status, ehci_batch->qh->current,
ehci_batch->qh->next, ehci_batch->qh->alternate);
if (!qh_halted(ehci_batch->qh) && (qh_transfer_pending(ehci_batch->qh) ||
qh_transfer_active(ehci_batch->qh)))
return false;
ehci_batch->base.transferred_size = ehci_batch->base.size;
for (size_t i = 0; i < ehci_batch->td_count; ++i) {
usb_log_debug("Batch %p: TD %zu: %08x:%08x:%08x.",
ehci_batch, i,
ehci_batch->tds[i].status, ehci_batch->tds[i].next,
ehci_batch->tds[i].alternate);
ehci_batch->base.error = td_error(&ehci_batch->tds[i]);
if (ehci_batch->base.error == EOK) {
ehci_batch->base.transferred_size -=
td_remain_size(&ehci_batch->tds[i]);
} else {
usb_log_debug("Batch %p found error TD(%zu):%08x: %s.",
ehci_batch, i,
ehci_batch->tds[i].status,
str_error_name(ehci_batch->base.error));
qh_clear_halt(ehci_batch->qh);
break;
}
}
assert(ehci_batch->base.transferred_size <= ehci_batch->base.size);
ehci_batch->qh->next = LINK_POINTER_TERM;
ehci_batch->qh->current = LINK_POINTER_TERM;
usb_log_debug("Batch %p complete: %s", ehci_batch,
str_error(ehci_batch->base.error));
return true;
}
void ehci_transfer_batch_commit(const ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
qh_set_next_td(ehci_batch->qh,
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[0]));
}
static void batch_control(ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
usb_direction_t dir = ehci_batch->base.dir;
assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
usb_log_debug2("Batch %p: Control QH(%p): "
"%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
ehci_batch->qh,
ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
ehci_batch->qh->status, ehci_batch->qh->current,
ehci_batch->qh->next, ehci_batch->qh->alternate);
static const usb_direction_t reverse_dir[] = {
[USB_DIRECTION_IN] = USB_DIRECTION_OUT,
[USB_DIRECTION_OUT] = USB_DIRECTION_IN,
};
int toggle = 0;
const usb_direction_t data_dir = dir;
const usb_direction_t status_dir = reverse_dir[dir];
td_init(&ehci_batch->tds[0],
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[1]),
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, ehci_batch->setup_buffer),
USB_DIRECTION_BOTH, USB_SETUP_PACKET_SIZE, toggle, false);
usb_log_debug2("Batch %p: Created CONTROL SETUP TD(%" PRIxn "): "
"%08x:%08x:%08x", ehci_batch,
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[0]),
ehci_batch->tds[0].status, ehci_batch->tds[0].next,
ehci_batch->tds[0].alternate);
unsigned td_current = 1;
size_t remain_size = ehci_batch->base.size;
uintptr_t buffer = dma_buffer_phys(&ehci_batch->base.dma_buffer,
ehci_batch->data_buffer);
while (remain_size > 0) {
const size_t transfer_size = min(remain_size, EHCI_TD_MAX_TRANSFER);
toggle = 1 - toggle;
td_init(&ehci_batch->tds[td_current],
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[td_current + 1]),
buffer, data_dir, transfer_size, toggle, false);
usb_log_debug2("Batch %p: Created CONTROL DATA TD(%" PRIxn "): "
"%08x:%08x:%08x", ehci_batch,
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[td_current]),
ehci_batch->tds[td_current].status,
ehci_batch->tds[td_current].next,
ehci_batch->tds[td_current].alternate);
buffer += transfer_size;
remain_size -= transfer_size;
assert(td_current < ehci_batch->td_count - 1);
++td_current;
}
assert(td_current == ehci_batch->td_count - 1);
td_init(&ehci_batch->tds[td_current], 0, 0, status_dir, 0, 1, true);
usb_log_debug2("Batch %p: Created CONTROL STATUS TD %d(%" PRIxn "): "
"%08x:%08x:%08x", ehci_batch, td_current,
dma_buffer_phys(&ehci_batch->ehci_dma_buffer, &ehci_batch->tds[td_current]),
ehci_batch->tds[td_current].status,
ehci_batch->tds[td_current].next,
ehci_batch->tds[td_current].alternate);
}
static void batch_data(ehci_transfer_batch_t *ehci_batch)
{
assert(ehci_batch);
usb_log_debug2("Batch %p: Data QH(%p): "
"%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
ehci_batch->qh,
ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
ehci_batch->qh->status, ehci_batch->qh->current,
ehci_batch->qh->next, ehci_batch->qh->alternate);
size_t td_current = 0;
size_t remain_size = ehci_batch->base.size;
uintptr_t buffer = dma_buffer_phys(&ehci_batch->base.dma_buffer,
ehci_batch->data_buffer);
while (remain_size > 0) {
const size_t transfer_size = remain_size > EHCI_TD_MAX_TRANSFER ?
EHCI_TD_MAX_TRANSFER : remain_size;
const bool last = (remain_size == transfer_size);
td_init(&ehci_batch->tds[td_current],
last ? 0 : dma_buffer_phys(&ehci_batch->ehci_dma_buffer,
&ehci_batch->tds[td_current + 1]),
buffer, ehci_batch->base.dir, transfer_size, -1, last);
usb_log_debug2("Batch %p: DATA TD(%" PRIxn ": %08x:%08x:%08x",
ehci_batch,
dma_buffer_phys(&ehci_batch->ehci_dma_buffer,
&ehci_batch->tds[td_current]),
ehci_batch->tds[td_current].status,
ehci_batch->tds[td_current].next,
ehci_batch->tds[td_current].alternate);
buffer += transfer_size;
remain_size -= transfer_size;
assert(td_current < ehci_batch->td_count);
++td_current;
}
}
static void (*const batch_setup[])(ehci_transfer_batch_t *) =
{
[USB_TRANSFER_CONTROL] = batch_control,
[USB_TRANSFER_BULK] = batch_data,
[USB_TRANSFER_INTERRUPT] = batch_data,
[USB_TRANSFER_ISOCHRONOUS] = NULL,
};
HelenOS homepage, sources at GitHub