/* remove queue from device schedule, dropping all data that came in */ static void xhci_destroy_intr_queue(endpoint_t *const ep, void *const q) { xhci_t *const xhci = XHCI_INST(ep->dev->controller); const int slot_id = ep->dev->address; const int ep_id = xhci_ep_id(ep); transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id]; intrq_t *const intrq = (intrq_t *)q; /* Make sure the endpoint is stopped */ if (EC_GET(STATE, xhci->dev[slot_id].ctx.ep[ep_id]) == 1) { const int cc = xhci_cmd_stop_endpoint(xhci, slot_id, ep_id); if (cc != CC_SUCCESS) xhci_debug("Warning: Failed to stop endpoint\n"); } /* Process all remaining transfer events */ xhci_handle_events(xhci); /* Free all pending transfers and the interrupt queue structure */ int i; for (i = 0; i < intrq->count; ++i) { free(phys_to_virt(intrq->next->ptr_low)); intrq->next = xhci_next_trb(intrq->next, NULL); } xhci->dev[slot_id].interrupt_queues[ep_id] = NULL; free((void *)intrq); /* Reset the controller's dequeue pointer and reinitialize the ring */ xhci_cmd_set_tr_dq(xhci, slot_id, ep_id, tr->ring, 1); xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE); }
/* read one intr-packet from queue, if available. extend the queue for new input. return NULL if nothing new available. Recommended use: while (data=poll_intr_queue(q)) process(data); */ static u8 * xhci_poll_intr_queue(void *const q) { if (!q) return NULL; intrq_t *const intrq = (intrq_t *)q; endpoint_t *const ep = intrq->ep; xhci_t *const xhci = XHCI_INST(ep->dev->controller); /* TODO: Reset interrupt queue if it gets halted? */ xhci_handle_events(xhci); u8 *reqdata = NULL; while (!reqdata && intrq->ready) { const int ep_id = xhci_ep_id(ep); transfer_ring_t *const tr = xhci->dev[ep->dev->address].transfer_rings[ep_id]; /* Fetch the request's buffer */ reqdata = phys_to_virt(intrq->next->ptr_low); /* Enqueue the last (spare) TRB and ring doorbell */ xhci_enqueue_trb(tr); xhci->dbreg[ep->dev->address] = ep_id; /* Reuse the current buffer for the next spare TRB */ xhci_clear_trb(tr->cur, tr->pcs); tr->cur->ptr_low = virt_to_phys(reqdata); tr->cur->ptr_high = 0; TRB_SET(TL, tr->cur, intrq->size); TRB_SET(TT, tr->cur, TRB_NORMAL); TRB_SET(ISP, tr->cur, 1); TRB_SET(IOC, tr->cur, 1); /* Check if anything was transferred */ const size_t read = TRB_GET(TL, intrq->next); if (!read) reqdata = NULL; else if (read < intrq->size) /* At least zero it, poll interface is rather limited */ memset(reqdata + read, 0x00, intrq->size - read); /* Advance the interrupt queue */ if (intrq->ready == intrq->next) /* This was last TRB being ready */ intrq->ready = NULL; intrq->next = xhci_next_trb(intrq->next, NULL); } return reqdata; }
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; }