HelenOS sources
This source file includes following definitions.
- hc_gen_irq_code
- hc_interrupt
- hc_add
- hc_start
- hc_setup_roothub
- hc_gone
- hc_init_hw
- create_transfer_batch
- destroy_transfer_batch
- endpoint_create
- endpoint_register
- endpoint_unregister
- device_enumerate
- device_gone
- hc_init_mem_structures
- hc_init_transfer_lists
- hc_status
- hc_schedule
- hc_debug_checker
#include <adt/list.h>
#include <assert.h>
#include <async.h>
#include <ddi.h>
#include <device/hw_res_parsed.h>
#include <fibril.h>
#include <errno.h>
#include <macros.h>
#include <mem.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <str_error.h>
#include <usb/debug.h>
#include <usb/usb.h>
#include <usb/host/utils/malloc32.h>
#include <usb/host/bandwidth.h>
#include <usb/host/utility.h>
#include "uhci_batch.h"
#include "transfer_list.h"
#include "hc.h"
#define UHCI_INTR_ALLOW_INTERRUPTS \
(UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
#define UHCI_STATUS_USED_INTERRUPTS \
(UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
static const irq_pio_range_t uhci_irq_pio_ranges[] = {
{
.base = 0,
.size = sizeof(uhci_regs_t)
}
};
static const irq_cmd_t uhci_irq_commands[] = {
{
.cmd = CMD_PIO_READ_16,
.dstarg = 1,
.addr = NULL
},
{
.cmd = CMD_AND,
.srcarg = 1,
.dstarg = 2,
.value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
},
{
.cmd = CMD_PREDICATE,
.srcarg = 2,
.value = 2
},
{
.cmd = CMD_PIO_WRITE_A_16,
.srcarg = 1,
.addr = NULL
},
{
.cmd = CMD_ACCEPT
}
};
static void hc_init_hw(const hc_t *instance);
static errno_t hc_init_mem_structures(hc_t *instance);
static errno_t hc_init_transfer_lists(hc_t *instance);
static errno_t hc_debug_checker(void *arg);
errno_t hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res, int *irq)
{
assert(code);
assert(hw_res);
if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
return EINVAL;
const addr_range_t regs = hw_res->io_ranges.ranges[0];
if (RNGSZ(regs) < sizeof(uhci_regs_t))
return EOVERFLOW;
code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
if (code->ranges == NULL)
return ENOMEM;
code->cmds = malloc(sizeof(uhci_irq_commands));
if (code->cmds == NULL) {
free(code->ranges);
return ENOMEM;
}
code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
code->ranges[0].base = RNGABS(regs);
memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
code->cmds[0].addr = (void *)®isters->usbsts;
code->cmds[3].addr = (void *)®isters->usbsts;
usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
*irq = hw_res->irqs.irqs[0];
return EOK;
}
static void hc_interrupt(bus_t *bus, uint32_t status)
{
hc_t *instance = bus_to_hc(bus);
if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
transfer_list_check_finished(&instance->transfers_interrupt);
transfer_list_check_finished(&instance->transfers_control_slow);
transfer_list_check_finished(&instance->transfers_control_full);
transfer_list_check_finished(&instance->transfers_bulk_full);
}
if (status & UHCI_STATUS_RESUME) {
usb_log_error("Resume interrupt!");
}
if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
usb_log_error("UHCI hardware failure!.");
++instance->hw_failures;
transfer_list_abort_all(&instance->transfers_interrupt);
transfer_list_abort_all(&instance->transfers_control_slow);
transfer_list_abort_all(&instance->transfers_control_full);
transfer_list_abort_all(&instance->transfers_bulk_full);
if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
hc_init_hw(instance);
} else {
usb_log_fatal("Too many UHCI hardware failures!.");
hc_gone(&instance->base);
}
}
}
errno_t hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
{
hc_t *instance = hcd_to_hc(hcd);
assert(hw_res);
if (hw_res->io_ranges.count != 1 ||
hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
return EINVAL;
instance->hw_failures = 0;
errno_t ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
(void **) &instance->registers);
if (ret != EOK) {
usb_log_error("Failed to gain access to registers: %s.",
str_error(ret));
return ret;
}
usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
hw_res->io_ranges.ranges[0].address.absolute,
hw_res->io_ranges.ranges[0].size);
ret = hc_init_mem_structures(instance);
if (ret != EOK) {
usb_log_error("Failed to init UHCI memory structures: %s.",
str_error(ret));
return ret;
}
return EOK;
}
int hc_start(hc_device_t *hcd)
{
hc_t *instance = hcd_to_hc(hcd);
hc_init_hw(instance);
(void)hc_debug_checker;
return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
}
int hc_setup_roothub(hc_device_t *hcd)
{
return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
}
int hc_gone(hc_device_t *instance)
{
assert(instance);
return ENOTSUP;
}
void hc_init_hw(const hc_t *instance)
{
assert(instance);
uhci_regs_t *registers = instance->registers;
pio_write_16(®isters->usbcmd, UHCI_CMD_GLOBAL_RESET);
fibril_usleep(50000);
pio_write_16(®isters->usbcmd, 0);
pio_write_16(®isters->usbcmd, UHCI_CMD_HCRESET);
do {
fibril_usleep(10);
} while ((pio_read_16(®isters->usbcmd) & UHCI_CMD_HCRESET) != 0);
pio_write_8(®isters->sofmod, 64);
const uint32_t pa = addr_to_phys(instance->frame_list);
pio_write_32(®isters->flbaseadd, pa);
if (cap_handle_valid(instance->base.irq_handle)) {
pio_write_16(&instance->registers->usbintr,
UHCI_INTR_ALLOW_INTERRUPTS);
}
const uint16_t cmd = pio_read_16(®isters->usbcmd);
if (cmd != 0)
usb_log_warning("Previous command value: %x.", cmd);
pio_write_16(®isters->usbcmd,
UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
}
static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
{
uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
return &batch->base;
}
static void destroy_transfer_batch(usb_transfer_batch_t *batch)
{
uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
}
static endpoint_t *endpoint_create(device_t *device, const usb_endpoint_descriptors_t *desc)
{
endpoint_t *ep = calloc(1, sizeof(uhci_endpoint_t));
if (ep)
endpoint_init(ep, device, desc);
return ep;
}
static errno_t endpoint_register(endpoint_t *ep)
{
hc_t *const hc = bus_to_hc(endpoint_get_bus(ep));
const errno_t err = usb2_bus_endpoint_register(&hc->bus_helper, ep);
if (err)
return err;
transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
if (!list)
return EOK;
endpoint_set_online(ep, &list->guard);
return EOK;
}
static void endpoint_unregister(endpoint_t *ep)
{
hc_t *const hc = bus_to_hc(endpoint_get_bus(ep));
usb2_bus_endpoint_unregister(&hc->bus_helper, ep);
if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
return;
}
transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
if (!list)
return;
fibril_mutex_lock(&list->guard);
endpoint_set_offline_locked(ep);
if (!ep->active_batch) {
fibril_mutex_unlock(&list->guard);
return;
}
endpoint_wait_timeout_locked(ep, 10000);
if (!ep->active_batch) {
fibril_mutex_unlock(&list->guard);
return;
}
uhci_transfer_batch_t *const batch =
uhci_transfer_batch_get(ep->active_batch);
endpoint_deactivate_locked(ep);
transfer_list_remove_batch(list, batch);
fibril_mutex_unlock(&list->guard);
fibril_usleep(20000);
batch->base.error = EINTR;
batch->base.transferred_size = 0;
usb_transfer_batch_finish(&batch->base);
}
static int device_enumerate(device_t *dev)
{
hc_t *const hc = bus_to_hc(dev->bus);
return usb2_bus_device_enumerate(&hc->bus_helper, dev);
}
static void device_gone(device_t *dev)
{
hc_t *const hc = bus_to_hc(dev->bus);
usb2_bus_device_gone(&hc->bus_helper, dev);
}
static int hc_status(bus_t *, uint32_t *);
static int hc_schedule(usb_transfer_batch_t *);
static const bus_ops_t uhci_bus_ops = {
.interrupt = hc_interrupt,
.status = hc_status,
.device_enumerate = device_enumerate,
.device_gone = device_gone,
.endpoint_create = endpoint_create,
.endpoint_register = endpoint_register,
.endpoint_unregister = endpoint_unregister,
.batch_create = create_transfer_batch,
.batch_schedule = hc_schedule,
.batch_destroy = destroy_transfer_batch,
};
errno_t hc_init_mem_structures(hc_t *instance)
{
assert(instance);
usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
bus_init(&instance->bus, sizeof(device_t));
instance->bus.ops = &uhci_bus_ops;
hc_device_setup(&instance->base, &instance->bus);
instance->frame_list = get_page();
if (!instance->frame_list) {
return ENOMEM;
}
usb_log_debug("Initialized frame list at %p.", instance->frame_list);
errno_t ret = hc_init_transfer_lists(instance);
if (ret != EOK) {
usb_log_error("Failed to initialize transfer lists.");
return_page(instance->frame_list);
return ENOMEM;
}
list_initialize(&instance->pending_endpoints);
usb_log_debug("Initialized transfer lists.");
const uint32_t queue = LINK_POINTER_QH(
addr_to_phys(instance->transfers_interrupt.queue_head));
for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
instance->frame_list[i] = queue;
}
return EOK;
}
errno_t hc_init_transfer_lists(hc_t *instance)
{
assert(instance);
#define SETUP_TRANSFER_LIST(type, name) \
do { \
errno_t ret = transfer_list_init(&instance->transfers_##type, name); \
if (ret != EOK) { \
usb_log_error("Failed to setup %s transfer list: %s.", \
name, str_error(ret)); \
transfer_list_fini(&instance->transfers_bulk_full); \
transfer_list_fini(&instance->transfers_control_full); \
transfer_list_fini(&instance->transfers_control_slow); \
transfer_list_fini(&instance->transfers_interrupt); \
return ret; \
} \
} while (0)
SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
#undef SETUP_TRANSFER_LIST
transfer_list_set_next(&instance->transfers_control_full,
&instance->transfers_bulk_full);
transfer_list_set_next(&instance->transfers_control_slow,
&instance->transfers_control_full);
transfer_list_set_next(&instance->transfers_interrupt,
&instance->transfers_control_slow);
#ifdef FSBR
transfer_list_set_next(&instance->transfers_bulk_full,
&instance->transfers_control_full);
#endif
instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
&instance->transfers_interrupt;
instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
&instance->transfers_interrupt;
instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
&instance->transfers_control_full;
instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
&instance->transfers_control_slow;
instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
&instance->transfers_bulk_full;
return EOK;
}
static errno_t hc_status(bus_t *bus, uint32_t *status)
{
hc_t *instance = bus_to_hc(bus);
assert(status);
*status = 0;
if (instance->registers) {
uint16_t s = pio_read_16(&instance->registers->usbsts);
pio_write_16(&instance->registers->usbsts, s);
*status = s;
}
return EOK;
}
static errno_t hc_schedule(usb_transfer_batch_t *batch)
{
uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
endpoint_t *ep = batch->ep;
hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
if (batch->target.address == uhci_rh_get_address(&hc->rh))
return uhci_rh_schedule(&hc->rh, batch);
transfer_list_t *const list =
hc->transfers[ep->device->speed][ep->transfer_type];
if (!list)
return ENOTSUP;
errno_t err;
if ((err = uhci_transfer_batch_prepare(uhci_batch)))
return err;
return transfer_list_add_batch(list, uhci_batch);
}
errno_t hc_debug_checker(void *arg)
{
hc_t *instance = arg;
assert(instance);
#define QH(queue) \
instance->transfers_##queue.queue_head
while (true) {
const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
const uint16_t sts = pio_read_16(&instance->registers->usbsts);
const uint16_t intr =
pio_read_16(&instance->registers->usbintr);
if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
usb_log_debug2("Command: %X Status: %X Intr: %x",
cmd, sts, intr);
}
const uintptr_t frame_list =
pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
if (frame_list != addr_to_phys(instance->frame_list)) {
usb_log_debug("Framelist address: %p vs. %p.",
(void *) frame_list,
(void *) addr_to_phys(instance->frame_list));
}
int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
uintptr_t expected_pa = instance->frame_list[frnum] &
LINK_POINTER_ADDRESS_MASK;
uintptr_t real_pa = addr_to_phys(QH(interrupt));
if (expected_pa != real_pa) {
usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
(void *) expected_pa, frnum, (void *) real_pa);
}
expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
real_pa = addr_to_phys(QH(control_slow));
if (expected_pa != real_pa) {
usb_log_debug("Control Slow QH: %p vs. %p.",
(void *) expected_pa, (void *) real_pa);
}
expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
real_pa = addr_to_phys(QH(control_full));
if (expected_pa != real_pa) {
usb_log_debug("Control Full QH: %p vs. %p.",
(void *) expected_pa, (void *) real_pa);
}
expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
real_pa = addr_to_phys(QH(bulk_full));
if (expected_pa != real_pa) {
usb_log_debug("Bulk QH: %p vs. %p.",
(void *) expected_pa, (void *) real_pa);
}
fibril_usleep(UHCI_DEBUGER_TIMEOUT);
}
return EOK;
#undef QH
}
HelenOS homepage, sources at GitHub