Add more logging levels
This commit is contained in:
+92
-81
@@ -199,10 +199,10 @@ static uint8_t tr_i = 0;
|
||||
static xhci_trb_t tr[RING_LENGTH + 1] __attribute__((aligned(PAGE_SIZE)));
|
||||
|
||||
static const xhci_trb_t *command_exec_sync(const xhci_trb_t *cmd) {
|
||||
LOG_LN("Queueing command TRB...");
|
||||
LOG_VAL_F(cmd->parameter, "%lx");
|
||||
LOG_VAL_F(cmd->status, "%x");
|
||||
LOG_VAL_F(cmd->control, "%x");
|
||||
LOG_LN_STEP("Queueing command TRB...");
|
||||
LOG_VAL_TRACE(cmd->parameter, "%lx");
|
||||
LOG_VAL_TRACE(cmd->status, "%x");
|
||||
LOG_VAL_TRACE(cmd->control, "%x");
|
||||
|
||||
xhci_trb_t *cre = &cr[cr_i];
|
||||
|
||||
@@ -213,12 +213,12 @@ static const xhci_trb_t *command_exec_sync(const xhci_trb_t *cmd) {
|
||||
XHCI_TRB_CYCLE_W((&cr[RING_LENGTH]), cr_cycle);
|
||||
cr_cycle ^= (cr_i == 0);
|
||||
|
||||
LOG_LN("Ringing command doorbell...");
|
||||
LOG_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_LN_STEP("Ringing command doorbell...");
|
||||
LOG_VAL_TRACE(op_regs->usbsts, "%x");
|
||||
db_regs[0] = 0;
|
||||
LOG_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_TRACE(op_regs->usbsts, "%x");
|
||||
|
||||
LOG_LN("Waiting for completion...");
|
||||
LOG_LN_STEP("Waiting for completion...");
|
||||
while (1) {
|
||||
xhci_trb_t *cmp = &er[er_i];
|
||||
while (XHCI_TRB_CYCLE_R(cmp) != er_cycle)
|
||||
@@ -229,75 +229,77 @@ static const xhci_trb_t *command_exec_sync(const xhci_trb_t *cmd) {
|
||||
er_cycle ^= (er_i == 0);
|
||||
|
||||
if (XHCI_TRB_TRB_TYPE_R(cmp) != XHCI_TRB_TYPE_EVENT_COMMAND_COMPLETION || (void *)cmp->parameter != VIRT_TO_PHYS(cre)) {
|
||||
LOG_LN("Received irrelevant event, skipping...");
|
||||
LOG_LN_STEP("Received irrelevant event, skipping...");
|
||||
} else {
|
||||
LOG_LN("Received command completion event...");
|
||||
LOG_LN_STEP("Received command completion event...");
|
||||
|
||||
LOG_VAL_F(cmp->parameter, "%lx");
|
||||
LOG_VAL_F(cmp->status, "%x");
|
||||
LOG_VAL_F(cmp->control, "%x");
|
||||
LOG_VAL_TRACE(cmp->parameter, "%lx");
|
||||
LOG_VAL_TRACE(cmp->status, "%x");
|
||||
LOG_VAL_TRACE(cmp->control, "%x");
|
||||
|
||||
ASSERT(XHCI_TRB_COMPLETION_CODE(cmp) == 1, "command_exec_sync: Command failed.");
|
||||
|
||||
LOG_LN_STEP("Done.");
|
||||
|
||||
return cmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xhci_init() {
|
||||
TRACE_LN("Searching for suitable controller...");
|
||||
LOG_LN_INFO("Searching for suitable controller...");
|
||||
pci_bdf_t bdf = pci_find_by_class(XHCI_PCI_CLASS, XHCI_PCI_SUBCLASS, XHCI_PCI_INTERFACE);
|
||||
ASSERT(bdf, "xhci_init: No suitable controllers found.");
|
||||
|
||||
TRACE_LN("Mapping controller to virtual memory...");
|
||||
LOG_LN_INFO("Mapping controller to virtual memory...");
|
||||
pci_map(bdf, cap_regs, 4);
|
||||
op_regs = (xhci_op_regs_t *)((uint8_t *)cap_regs + cap_regs->caplength);
|
||||
port_regs = (xhci_port_regs_t *)((uint8_t *)cap_regs + cap_regs->caplength + 0x400);
|
||||
intr_regs = (xhci_intr_regs_t *)((uint8_t *)cap_regs + cap_regs->rtsoff + 0x20);
|
||||
db_regs = (uint32_t *)((uint8_t *)cap_regs + cap_regs->dboff);
|
||||
|
||||
TRACE_VAL_F(cap_regs->caplength, "%hhx");
|
||||
TRACE_VAL_F(cap_regs->hciversion, "%hx");
|
||||
TRACE_VAL_F(cap_regs->hcsparams1, "%x");
|
||||
TRACE_VAL_F(cap_regs->hcsparams2, "%x");
|
||||
TRACE_VAL_F(cap_regs->hcsparams3, "%x");
|
||||
TRACE_VAL_F(cap_regs->hccparams1, "%x");
|
||||
TRACE_VAL_F(cap_regs->dboff, "%x");
|
||||
TRACE_VAL_F(cap_regs->rtsoff, "%x");
|
||||
TRACE_VAL_F(cap_regs->hccparams2, "%x");
|
||||
LOG_VAL_DATA(cap_regs->caplength, "%hhx");
|
||||
LOG_VAL_DATA(cap_regs->hciversion, "%hx");
|
||||
LOG_VAL_DATA(cap_regs->hcsparams1, "%x");
|
||||
LOG_VAL_DATA(cap_regs->hcsparams2, "%x");
|
||||
LOG_VAL_DATA(cap_regs->hcsparams3, "%x");
|
||||
LOG_VAL_DATA(cap_regs->hccparams1, "%x");
|
||||
LOG_VAL_DATA(cap_regs->dboff, "%x");
|
||||
LOG_VAL_DATA(cap_regs->rtsoff, "%x");
|
||||
LOG_VAL_DATA(cap_regs->hccparams2, "%x");
|
||||
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
TRACE_VAL_F(op_regs->pagesize, "%x");
|
||||
TRACE_VAL_F(op_regs->dnctrl, "%x");
|
||||
TRACE_VAL_F(op_regs->crcr, "%lx");
|
||||
TRACE_VAL_F(op_regs->dcbaap, "%lx");
|
||||
TRACE_VAL_F(op_regs->config, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->pagesize, "%x");
|
||||
LOG_VAL_DATA(op_regs->dnctrl, "%x");
|
||||
LOG_VAL_DATA(op_regs->crcr, "%lx");
|
||||
LOG_VAL_DATA(op_regs->dcbaap, "%lx");
|
||||
LOG_VAL_DATA(op_regs->config, "%x");
|
||||
|
||||
TRACE_LN("Stopping controller...");
|
||||
LOG_LN_INFO("Stopping controller...");
|
||||
XHCI_OP_REGS_RS_W(op_regs, 0);
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
while (!XHCI_OP_REGS_HCH(op_regs))
|
||||
;
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
|
||||
TRACE_LN("Resetting controller...");
|
||||
LOG_LN_INFO("Resetting controller...");
|
||||
XHCI_OP_REGS_EINT_W(op_regs, 0);
|
||||
XHCI_OP_REGS_HCRST_W(op_regs, 1);
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
while (XHCI_OP_REGS_HCRST_R(op_regs))
|
||||
;
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
while (XHCI_OP_REGS_CNR(op_regs))
|
||||
;
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
|
||||
TRACE_LN("Setting up command ring...");
|
||||
LOG_LN_INFO("Setting up command ring...");
|
||||
xhci_trb_t *wrapper = &cr[RING_LENGTH];
|
||||
wrapper->parameter = (uint64_t)VIRT_TO_PHYS(cr);
|
||||
wrapper->status = 0;
|
||||
@@ -305,7 +307,7 @@ void xhci_init() {
|
||||
XHCI_TRB_TOGGLE_CYCLE_W(wrapper, 1);
|
||||
XHCI_TRB_CYCLE_W(wrapper, cr_cycle);
|
||||
|
||||
TRACE_LN("Configuring controller...");
|
||||
LOG_LN_INFO("Configuring controller...");
|
||||
XHCI_OP_REGS_MAX_SLOTS_EN_W(op_regs, MAX_SLOTS);
|
||||
op_regs->dcbaap = (uint64_t)VIRT_TO_PHYS(dcbaa);
|
||||
op_regs->crcr = (uint64_t)VIRT_TO_PHYS(cr) | cr_cycle;
|
||||
@@ -315,30 +317,29 @@ void xhci_init() {
|
||||
intr_regs->erstba = (uint64_t)VIRT_TO_PHYS(erst);
|
||||
intr_regs->erdp = erst[0].base;
|
||||
|
||||
TRACE_VAL_F(op_regs->config, "%x");
|
||||
TRACE_VAL_F(op_regs->dcbaap, "%lx");
|
||||
TRACE_VAL_F(op_regs->crcr, "%lx");
|
||||
TRACE_VAL_F(intr_regs->erstba, "%lx");
|
||||
TRACE_VAL_F(intr_regs->erstsz, "%x");
|
||||
TRACE_VAL_F(intr_regs->erdp, "%lx");
|
||||
TRACE_VAL_F(erst[0].size, "%hx");
|
||||
TRACE_VAL_F(erst[0].base, "%lx");
|
||||
LOG_VAL_DATA(op_regs->config, "%x");
|
||||
LOG_VAL_DATA(op_regs->dcbaap, "%lx");
|
||||
LOG_VAL_DATA(op_regs->crcr, "%lx");
|
||||
LOG_VAL_DATA(intr_regs->erstba, "%lx");
|
||||
LOG_VAL_DATA(intr_regs->erstsz, "%x");
|
||||
LOG_VAL_DATA(intr_regs->erdp, "%lx");
|
||||
LOG_VAL_DATA(erst[0].size, "%hx");
|
||||
LOG_VAL_DATA(erst[0].base, "%lx");
|
||||
|
||||
TRACE_LN("Starting controller...");
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_LN_INFO("Starting controller...");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
XHCI_OP_REGS_RS_W(op_regs, 1);
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
while (XHCI_OP_REGS_HCH(op_regs))
|
||||
;
|
||||
TRACE_VAL_F(op_regs->usbcmd, "%x");
|
||||
TRACE_VAL_F(op_regs->usbsts, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbcmd, "%x");
|
||||
LOG_VAL_DATA(op_regs->usbsts, "%x");
|
||||
|
||||
LOG_LN("Enumerating ports...");
|
||||
PRINT_LN(kernel_log, "Enumerating ports...");
|
||||
xhci_enumerate(kernel_log);
|
||||
|
||||
LOG_LN("Done.");
|
||||
PRINT_LN(kernel_log, "Done.");
|
||||
}
|
||||
|
||||
void xhci_enumerate(stream_t *out) {
|
||||
@@ -357,6 +358,8 @@ uint8_t xhci_port_connected(uint8_t port) {
|
||||
}
|
||||
|
||||
xhci_slot_t xhci_attach(uint8_t port) {
|
||||
LOG_LN_STEP("Attaching port %d ...", port);
|
||||
|
||||
ASSERT(!tr_attached_slot, "xhci_attach: Can only attach one port at a time.")
|
||||
ASSERT(XHCI_PORT_REGS_CCS(&port_regs[port]), "xhci_attach: Port is not connected.");
|
||||
|
||||
@@ -364,21 +367,21 @@ xhci_slot_t xhci_attach(uint8_t port) {
|
||||
tr_i = 0;
|
||||
tr_cycle = 1;
|
||||
|
||||
LOG_LN("Resetting device...");
|
||||
LOG_LN_STEP("Resetting device...");
|
||||
port_regs[port].portsc = 1 << 4;
|
||||
while (XHCI_PORT_REGS_PR_R(&port_regs[port]))
|
||||
;
|
||||
while (!XHCI_PORT_REGS_PED(&port_regs[port]))
|
||||
;
|
||||
TRACE_VAL_F(XHCI_PORT_REGS_PED(&port_regs[port]), "%hhx");
|
||||
LOG_VAL_TRACE(XHCI_PORT_REGS_PED(&port_regs[port]), "%hhx");
|
||||
|
||||
LOG_LN("Enabling a slot...");
|
||||
LOG_LN_STEP("Enabling a slot...");
|
||||
xhci_trb_t cmd_es;
|
||||
memory_set(0, sizeof(xhci_trb_t), &cmd_es);
|
||||
XHCI_TRB_TRB_TYPE_W(&cmd_es, XHCI_TRB_TYPE_COMMAND_ENABLE_SLOT);
|
||||
xhci_slot_t slot = XHCI_TRB_SLOT_ID_R(command_exec_sync(&cmd_es));
|
||||
|
||||
LOG_LN("Addressing device...");
|
||||
LOG_LN_STEP("Addressing device...");
|
||||
memory_set(0, sizeof(input_ctx), &input_ctx);
|
||||
input_ctx.control.add_flags = 0b11;
|
||||
XHCI_SLOT_CTX_CTX_ENTRIES_W(&input_ctx.slot, 1);
|
||||
@@ -399,13 +402,15 @@ xhci_slot_t xhci_attach(uint8_t port) {
|
||||
|
||||
tr_attached_slot = slot;
|
||||
|
||||
LOG_LN_STEP("Done.");
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
uint64_t xhci_control_transfer(xhci_slot_t slot, uint64_t setup, void *data, uint16_t length) {
|
||||
ASSERT(tr_attached_slot == slot, "xhci_control_transfer: Can only attach one port at a time.");
|
||||
|
||||
LOG_LN("Queueing transfer TRBs...");
|
||||
LOG_LN_STEP("Queueing transfer TRBs...");
|
||||
|
||||
tr[tr_i].parameter = setup;
|
||||
tr[tr_i].status = sizeof(setup);
|
||||
@@ -414,9 +419,9 @@ uint64_t xhci_control_transfer(xhci_slot_t slot, uint64_t setup, void *data, uin
|
||||
BITS_W(tr[tr_i].control, 6, 6, 1);
|
||||
XHCI_TRB_CYCLE_W(&tr[tr_i], tr_cycle);
|
||||
|
||||
LOG_VAL_F(tr[tr_i].parameter, "%lx");
|
||||
LOG_VAL_F(tr[tr_i].status, "%x");
|
||||
LOG_VAL_F(tr[tr_i].control, "%x");
|
||||
LOG_VAL_TRACE(tr[tr_i].parameter, "%lx");
|
||||
LOG_VAL_TRACE(tr[tr_i].status, "%x");
|
||||
LOG_VAL_TRACE(tr[tr_i].control, "%x");
|
||||
|
||||
tr_i = (tr_i + 1) % RING_LENGTH;
|
||||
XHCI_TRB_CYCLE_W(&tr[RING_LENGTH], tr_cycle);
|
||||
@@ -428,9 +433,9 @@ uint64_t xhci_control_transfer(xhci_slot_t slot, uint64_t setup, void *data, uin
|
||||
BITS_W(tr[tr_i].control, 16, 16, 1);
|
||||
XHCI_TRB_CYCLE_W(&tr[tr_i], tr_cycle);
|
||||
|
||||
LOG_VAL_F(tr[tr_i].parameter, "%lx");
|
||||
LOG_VAL_F(tr[tr_i].status, "%x");
|
||||
LOG_VAL_F(tr[tr_i].control, "%x");
|
||||
LOG_VAL_TRACE(tr[tr_i].parameter, "%lx");
|
||||
LOG_VAL_TRACE(tr[tr_i].status, "%x");
|
||||
LOG_VAL_TRACE(tr[tr_i].control, "%x");
|
||||
|
||||
tr_i = (tr_i + 1) % RING_LENGTH;
|
||||
XHCI_TRB_CYCLE_W(&tr[RING_LENGTH], tr_cycle);
|
||||
@@ -447,10 +452,10 @@ uint64_t xhci_control_transfer(xhci_slot_t slot, uint64_t setup, void *data, uin
|
||||
XHCI_TRB_CYCLE_W(&tr[RING_LENGTH], tr_cycle);
|
||||
tr_cycle ^= (tr_i == 0);
|
||||
|
||||
LOG_LN("Ringing slot doorbell...");
|
||||
LOG_LN_STEP("Ringing slot doorbell...");
|
||||
db_regs[tr_attached_slot] = 1;
|
||||
|
||||
LOG_LN("Waiting for completion...");
|
||||
LOG_LN_STEP("Waiting for completion...");
|
||||
while (1) {
|
||||
xhci_trb_t *cmp = &er[er_i];
|
||||
while (XHCI_TRB_CYCLE_R(cmp) != er_cycle)
|
||||
@@ -461,22 +466,26 @@ uint64_t xhci_control_transfer(xhci_slot_t slot, uint64_t setup, void *data, uin
|
||||
er_cycle ^= (er_i == 0);
|
||||
|
||||
if (XHCI_TRB_TRB_TYPE_R(cmp) != XHCI_TRB_TYPE_EVENT_TRANSFER_COMPLETION || (void *)cmp->parameter != VIRT_TO_PHYS(tre)) {
|
||||
LOG_LN("Received irrelevant event, skipping...");
|
||||
LOG_LN_STEP("Received irrelevant event, skipping...");
|
||||
} else {
|
||||
LOG_LN("Received transfer completion event...");
|
||||
LOG_LN_STEP("Received transfer completion event...");
|
||||
|
||||
LOG_VAL_F(cmp->parameter, "%lx");
|
||||
LOG_VAL_F(cmp->status, "%x");
|
||||
LOG_VAL_F(cmp->control, "%x");
|
||||
LOG_VAL_TRACE(cmp->parameter, "%lx");
|
||||
LOG_VAL_TRACE(cmp->status, "%x");
|
||||
LOG_VAL_TRACE(cmp->control, "%x");
|
||||
|
||||
ASSERT(XHCI_TRB_COMPLETION_CODE(cmp) == 1, "xhci_control_transfer: Transfer failed.");
|
||||
|
||||
LOG_LN_STEP("Done.");
|
||||
|
||||
return length - (cmp->status & 0xFFFFFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xhci_detach(xhci_slot_t slot) {
|
||||
LOG_LN_STEP("Detaching slot %d...", slot);
|
||||
|
||||
ASSERT(tr_attached_slot == slot, "xhci_detach: Slot not attached.");
|
||||
|
||||
xhci_trb_t cmd_ds;
|
||||
@@ -487,4 +496,6 @@ void xhci_detach(xhci_slot_t slot) {
|
||||
|
||||
dcbaa[slot] = 0;
|
||||
tr_attached_slot = 0;
|
||||
|
||||
LOG_LN_STEP("Done.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user