/* handle standard types: * - command completion event * - port status change event * - transfer event * - host controller event */ static void xhci_handle_event(xhci_t *const xhci) { const trb_t *const ev = xhci->er.cur; const int trb_type = TRB_GET(TT, ev); switch (trb_type) { /* Either pass along the event or advance event ring */ case TRB_EV_TRANSFER: xhci_handle_transfer_event(xhci); break; case TRB_EV_CMD_CMPL: xhci_handle_command_completion_event(xhci); break; case TRB_EV_PORTSC: xhci_debug("Port Status Change Event for %d: %d\n", TRB_GET(PORT, ev), TRB_GET(CC, ev)); /* We ignore the event as we look for the PORTSC registers instead, at a time when it suits _us_. */ xhci_advance_event_ring(xhci); break; case TRB_EV_HOST: xhci_handle_host_controller_event(xhci); break; default: xhci_debug("Warning: Spurious event: %d, Completion Code: %d\n", trb_type, TRB_GET(CC, ev)); xhci_advance_event_ring(xhci); break; } }
static void xhci_handle_host_controller_event(xhci_t *const xhci) { const trb_t *const ev = xhci->er.cur; const int cc = TRB_GET(CC, ev); switch (cc) { case CC_EVENT_RING_FULL_ERROR: xhci_debug("Event ring full! (@%p)\n", xhci->er.cur); /* * If we get here, we have processed the whole queue: * xHC pushes this event, when it sees the ring full, * full of other events. * IMO it's save and necessary to update the dequeue * pointer here. */ xhci_advance_event_ring(xhci); xhci_update_event_dq(xhci); break; default: xhci_debug("Warning: Spurious host controller event: %d\n", cc); xhci_advance_event_ring(xhci); break; } }
/* * returns cc of command in question (pointed to by `address`) * caller should abort command if cc is TIMEOUT */ int xhci_wait_for_command_done(xhci_t *const xhci, const trb_t *const address, const int clear_event) { /* * The Address Device Command should take most time, as it has to * communicate with the USB device. Set address processing shouldn't * take longer than 50ms (at the slave). Let's take a timeout of * 100ms. */ unsigned long timeout_us = 100 * 1000; /* 100ms */ int cc = TIMEOUT; while (xhci_wait_for_event_type(xhci, TRB_EV_CMD_CMPL, &timeout_us)) { if ((xhci->er.cur->ptr_low == virt_to_phys(address)) && (xhci->er.cur->ptr_high == 0)) { cc = TRB_GET(CC, xhci->er.cur); break; } xhci_handle_command_completion_event(xhci); } if (!timeout_us) { xhci_debug("Warning: Timed out waiting for TRB_EV_CMD_CMPL.\n"); } else if (clear_event) { xhci_advance_event_ring(xhci); } xhci_update_event_dq(xhci); return cc; }
/* returns cc of command in question (pointed to by `address`) */ int xhci_wait_for_command_aborted(xhci_t *const xhci, const trb_t *const address) { /* * Specification says that something might be seriously wrong, if * we don't get a response after 5s. Still, let the caller decide, * what to do then. */ unsigned long timeout_us = 5 * 1000 * 1000; /* 5s */ int cc = TIMEOUT; /* * Expects two command completion events: * The first with CC == COMMAND_ABORTED should point to address, * the second with CC == COMMAND_RING_STOPPED should point to new dq. */ while (xhci_wait_for_event_type(xhci, TRB_EV_CMD_CMPL, &timeout_us)) { if ((xhci->er.cur->ptr_low == virt_to_phys(address)) && (xhci->er.cur->ptr_high == 0)) { cc = TRB_GET(CC, xhci->er.cur); xhci_advance_event_ring(xhci); break; } xhci_handle_command_completion_event(xhci); } if (!timeout_us) xhci_debug("Warning: Timed out waiting for COMMAND_ABORTED.\n"); while (xhci_wait_for_event_type(xhci, TRB_EV_CMD_CMPL, &timeout_us)) { if (TRB_GET(CC, xhci->er.cur) == CC_COMMAND_RING_STOPPED) { xhci->cr.cur = phys_to_virt(xhci->er.cur->ptr_low); xhci_advance_event_ring(xhci); break; } xhci_handle_command_completion_event(xhci); } if (!timeout_us) xhci_debug("Warning: Timed out " "waiting for COMMAND_RING_STOPPED.\n"); xhci_update_event_dq(xhci); return cc; }
static void xhci_handle_command_completion_event(xhci_t *const xhci) { const trb_t *const ev = xhci->er.cur; xhci_debug("Warning: Spurious command completion event:\n" " Pointer: 0x%08x%08x\n" " CC: %d\n" " Slot ID: %d\n" " Cycle: %d\n", ev->ptr_high, ev->ptr_low, TRB_GET(CC, ev), TRB_GET(ID, ev), ev->control & TRB_CYCLE); xhci_advance_event_ring(xhci); }
int xhci_cmd_enable_slot(xhci_t *const xhci, int *const slot_id) { trb_t *const cmd = xhci_next_command_trb(xhci); TRB_SET(TT, cmd, TRB_CMD_ENABLE_SLOT); xhci_post_command(xhci); int cc = xhci_wait_for_command(xhci, cmd, 0); if (cc >= 0) { if (cc == CC_SUCCESS) { *slot_id = TRB_GET(ID, xhci->er.cur); if (*slot_id > xhci->max_slots_en) cc = CONTROLLER_ERROR; } xhci_advance_event_ring(xhci); xhci_handle_events(xhci); } return cc; }
static void xhci_handle_transfer_event(xhci_t *const xhci) { const trb_t *const ev = xhci->er.cur; const int cc = TRB_GET(CC, ev); const int id = TRB_GET(ID, ev); const int ep = TRB_GET(EP, ev); devinfo_t *di; intrq_t *intrq; if (id && id <= xhci->max_slots_en && (di = DEVINFO_FROM_XHCI(xhci, id)) && (intrq = di->interrupt_queues[ep])) { /* It's a running interrupt endpoint */ intrq->ready = phys_to_virt(ev->ptr_low); if (cc == CC_SUCCESS || cc == CC_SHORT_PACKET) { TRB_SET(TL, intrq->ready, intrq->size - TRB_GET(EVTL, ev)); } else { xhci_debug("Interrupt Transfer failed: %d\n", cc); TRB_SET(TL, intrq->ready, 0); } } else if (cc == CC_STOPPED || cc == CC_STOPPED_LENGTH_INVALID) { /* Ignore 'Forced Stop Events' */ } else { xhci_debug("Warning: " "Spurious transfer event for ID %d, EP %d:\n" " Pointer: 0x%08x%08x\n" " TL: 0x%06x\n" " CC: %d\n", id, ep, ev->ptr_high, ev->ptr_low, TRB_GET(EVTL, ev), cc); } xhci_advance_event_ring(xhci); }
/* returns cc of transfer for given slot/endpoint pair */ int xhci_wait_for_transfer(xhci_t *const xhci, const int slot_id, const int ep_id) { xhci_spew("Waiting for transfer on ID %d EP %d\n", slot_id, ep_id); /* 2s for all types of transfers */ /* TODO: test, wait longer? */ unsigned long timeout_us = 2 * 1000 * 1000; int cc = TIMEOUT; while (xhci_wait_for_event_type(xhci, TRB_EV_TRANSFER, &timeout_us)) { if (TRB_GET(ID, xhci->er.cur) == slot_id && TRB_GET(EP, xhci->er.cur) == ep_id) { cc = TRB_GET(CC, xhci->er.cur); xhci_advance_event_ring(xhci); break; } xhci_handle_transfer_event(xhci); } if (!timeout_us) xhci_debug("Warning: Timed out waiting for TRB_EV_TRANSFER.\n"); xhci_update_event_dq(xhci); return cc; }