Merge remote-tracking branch 'kraxel/usb.42' into staging

* kraxel/usb.42:
  xhci: fix port status
  xhci: fix control xfers
  usb: add shortcut for control transfers
  usb-host: enable pipelineing for bulk endpoints.
  usb: add pipelining option to usb endpoints
  usb: queue can have async packets
  uhci_fill_queue: zap debug printf
  usb: add USB_RET_IOERROR
  usb: return BABBLE rather then NAK when we receive too much data
  usb-ehci: Cleanup itd error handling
  usb-ehci: Fix and simplify nakcnt handling
  usb-ehci: Remove dead nakcnt code
  usb-ehci: Fix cerr tracking
  usb-ehci: Any packet completion except for NAK should set the interrupt
  usb-ehci: Rip the queues when the async or period schedule is halted
  usb-ehci: Drop cached qhs when the doorbell gets rung
  usb-ehci: always call ehci_queues_rip_unused for period queues
  usb-ehci: split our qh queue into async and periodic queues
  usb-ehci: Never follow table entries with the T-bit set
  usb-redir: Set ep type and interface
This commit is contained in:
Anthony Liguori 2012-03-09 12:29:40 -06:00
commit 9f1d43b133
8 changed files with 203 additions and 143 deletions

View File

@ -347,7 +347,6 @@ enum async_state {
struct EHCIQueue { struct EHCIQueue {
EHCIState *ehci; EHCIState *ehci;
QTAILQ_ENTRY(EHCIQueue) next; QTAILQ_ENTRY(EHCIQueue) next;
bool async_schedule;
uint32_t seen; uint32_t seen;
uint64_t ts; uint64_t ts;
@ -367,6 +366,8 @@ struct EHCIQueue {
int usb_status; int usb_status;
}; };
typedef QTAILQ_HEAD(EHCIQueueHead, EHCIQueue) EHCIQueueHead;
struct EHCIState { struct EHCIState {
PCIDevice dev; PCIDevice dev;
USBBus bus; USBBus bus;
@ -410,7 +411,8 @@ struct EHCIState {
USBPort ports[NB_PORTS]; USBPort ports[NB_PORTS];
USBPort *companion_ports[NB_PORTS]; USBPort *companion_ports[NB_PORTS];
uint32_t usbsts_pending; uint32_t usbsts_pending;
QTAILQ_HEAD(, EHCIQueue) queues; EHCIQueueHead aqueues;
EHCIQueueHead pqueues;
uint32_t a_fetch_addr; // which address to look at next uint32_t a_fetch_addr; // which address to look at next
uint32_t p_fetch_addr; // which address to look at next uint32_t p_fetch_addr; // which address to look at next
@ -660,31 +662,34 @@ static void ehci_trace_sitd(EHCIState *s, target_phys_addr_t addr,
static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, int async) static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, int async)
{ {
EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
EHCIQueue *q; EHCIQueue *q;
q = g_malloc0(sizeof(*q)); q = g_malloc0(sizeof(*q));
q->ehci = ehci; q->ehci = ehci;
q->async_schedule = async; QTAILQ_INSERT_HEAD(head, q, next);
QTAILQ_INSERT_HEAD(&ehci->queues, q, next);
trace_usb_ehci_queue_action(q, "alloc"); trace_usb_ehci_queue_action(q, "alloc");
return q; return q;
} }
static void ehci_free_queue(EHCIQueue *q) static void ehci_free_queue(EHCIQueue *q, int async)
{ {
EHCIQueueHead *head = async ? &q->ehci->aqueues : &q->ehci->pqueues;
trace_usb_ehci_queue_action(q, "free"); trace_usb_ehci_queue_action(q, "free");
if (q->async == EHCI_ASYNC_INFLIGHT) { if (q->async == EHCI_ASYNC_INFLIGHT) {
usb_cancel_packet(&q->packet); usb_cancel_packet(&q->packet);
} }
QTAILQ_REMOVE(&q->ehci->queues, q, next); QTAILQ_REMOVE(head, q, next);
g_free(q); g_free(q);
} }
static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr) static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
int async)
{ {
EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
EHCIQueue *q; EHCIQueue *q;
QTAILQ_FOREACH(q, &ehci->queues, next) { QTAILQ_FOREACH(q, head, next) {
if (addr == q->qhaddr) { if (addr == q->qhaddr) {
return q; return q;
} }
@ -692,43 +697,46 @@ static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr)
return NULL; return NULL;
} }
static void ehci_queues_rip_unused(EHCIState *ehci) static void ehci_queues_rip_unused(EHCIState *ehci, int async, int flush)
{ {
EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
EHCIQueue *q, *tmp; EHCIQueue *q, *tmp;
QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) { QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
if (q->seen) { if (q->seen) {
q->seen = 0; q->seen = 0;
q->ts = ehci->last_run_ns; q->ts = ehci->last_run_ns;
continue; continue;
} }
if (ehci->last_run_ns < q->ts + 250000000) { if (!flush && ehci->last_run_ns < q->ts + 250000000) {
/* allow 0.25 sec idle */ /* allow 0.25 sec idle */
continue; continue;
} }
ehci_free_queue(q); ehci_free_queue(q, async);
} }
} }
static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev) static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
{ {
EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
EHCIQueue *q, *tmp; EHCIQueue *q, *tmp;
QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) { QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
if (!usb_packet_is_inflight(&q->packet) || if (!usb_packet_is_inflight(&q->packet) ||
q->packet.ep->dev != dev) { q->packet.ep->dev != dev) {
continue; continue;
} }
ehci_free_queue(q); ehci_free_queue(q, async);
} }
} }
static void ehci_queues_rip_all(EHCIState *ehci) static void ehci_queues_rip_all(EHCIState *ehci, int async)
{ {
EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
EHCIQueue *q, *tmp; EHCIQueue *q, *tmp;
QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) { QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
ehci_free_queue(q); ehci_free_queue(q, async);
} }
} }
@ -773,7 +781,8 @@ static void ehci_detach(USBPort *port)
return; return;
} }
ehci_queues_rip_device(s, port->dev); ehci_queues_rip_device(s, port->dev, 0);
ehci_queues_rip_device(s, port->dev, 1);
*portsc &= ~(PORTSC_CONNECT|PORTSC_PED); *portsc &= ~(PORTSC_CONNECT|PORTSC_PED);
*portsc |= PORTSC_CSC; *portsc |= PORTSC_CSC;
@ -793,7 +802,8 @@ static void ehci_child_detach(USBPort *port, USBDevice *child)
return; return;
} }
ehci_queues_rip_device(s, child); ehci_queues_rip_device(s, child, 0);
ehci_queues_rip_device(s, child, 1);
} }
static void ehci_wakeup(USBPort *port) static void ehci_wakeup(USBPort *port)
@ -911,7 +921,8 @@ static void ehci_reset(void *opaque)
usb_device_reset(devs[i]); usb_device_reset(devs[i]);
} }
} }
ehci_queues_rip_all(s); ehci_queues_rip_all(s, 0);
ehci_queues_rip_all(s, 1);
qemu_del_timer(s->frame_timer); qemu_del_timer(s->frame_timer);
} }
@ -1065,7 +1076,8 @@ static void ehci_mem_writel(void *ptr, target_phys_addr_t addr, uint32_t val)
if (!(val & USBCMD_RUNSTOP) && (s->usbcmd & USBCMD_RUNSTOP)) { if (!(val & USBCMD_RUNSTOP) && (s->usbcmd & USBCMD_RUNSTOP)) {
qemu_del_timer(s->frame_timer); qemu_del_timer(s->frame_timer);
// TODO - should finish out some stuff before setting halt ehci_queues_rip_all(s, 0);
ehci_queues_rip_all(s, 1);
ehci_set_usbsts(s, USBSTS_HALT); ehci_set_usbsts(s, USBSTS_HALT);
} }
@ -1279,8 +1291,6 @@ static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
static void ehci_execute_complete(EHCIQueue *q) static void ehci_execute_complete(EHCIQueue *q)
{ {
int c_err, reload;
assert(q->async != EHCI_ASYNC_INFLIGHT); assert(q->async != EHCI_ASYNC_INFLIGHT);
q->async = EHCI_ASYNC_NONE; q->async = EHCI_ASYNC_NONE;
@ -1288,15 +1298,11 @@ static void ehci_execute_complete(EHCIQueue *q)
q->qhaddr, q->qh.next, q->qtdaddr, q->usb_status); q->qhaddr, q->qh.next, q->qtdaddr, q->usb_status);
if (q->usb_status < 0) { if (q->usb_status < 0) {
err:
/* TO-DO: put this is in a function that can be invoked below as well */
c_err = get_field(q->qh.token, QTD_TOKEN_CERR);
c_err--;
set_field(&q->qh.token, c_err, QTD_TOKEN_CERR);
switch(q->usb_status) { switch(q->usb_status) {
case USB_RET_IOERROR:
case USB_RET_NODEV: case USB_RET_NODEV:
q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR); q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
ehci_record_interrupt(q->ehci, USBSTS_ERRINT); ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
break; break;
case USB_RET_STALL: case USB_RET_STALL:
@ -1304,16 +1310,8 @@ err:
ehci_record_interrupt(q->ehci, USBSTS_ERRINT); ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
break; break;
case USB_RET_NAK: case USB_RET_NAK:
/* 4.10.3 */ set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT);
reload = get_field(q->qh.epchar, QH_EPCHAR_RL); return; /* We're not done yet with this transaction */
if ((q->pid == USB_TOKEN_IN) && reload) {
int nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
nakcnt--;
set_field(&q->qh.altnext_qtd, nakcnt, QH_ALTNEXT_NAKCNT);
} else if (!reload) {
return;
}
break;
case USB_RET_BABBLE: case USB_RET_BABBLE:
q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE); q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
ehci_record_interrupt(q->ehci, USBSTS_ERRINT); ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
@ -1324,15 +1322,13 @@ err:
assert(0); assert(0);
break; break;
} }
} else if ((q->usb_status > q->tbytes) && (q->pid == USB_TOKEN_IN)) {
q->usb_status = USB_RET_BABBLE;
q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
} else { } else {
// DPRINTF("Short packet condition\n");
// TODO check 4.12 for splits // TODO check 4.12 for splits
if ((q->usb_status > q->tbytes) && (q->pid == USB_TOKEN_IN)) {
q->usb_status = USB_RET_BABBLE;
goto err;
}
if (q->tbytes && q->pid == USB_TOKEN_IN) { if (q->tbytes && q->pid == USB_TOKEN_IN) {
q->tbytes -= q->usb_status; q->tbytes -= q->usb_status;
} else { } else {
@ -1348,7 +1344,7 @@ err:
q->qh.token ^= QTD_TOKEN_DTOGGLE; q->qh.token ^= QTD_TOKEN_DTOGGLE;
q->qh.token &= ~QTD_TOKEN_ACTIVE; q->qh.token &= ~QTD_TOKEN_ACTIVE;
if ((q->usb_status >= 0) && (q->qh.token & QTD_TOKEN_IOC)) { if (q->qh.token & QTD_TOKEN_IOC) {
ehci_record_interrupt(q->ehci, USBSTS_INT); ehci_record_interrupt(q->ehci, USBSTS_INT);
} }
} }
@ -1471,24 +1467,12 @@ static int ehci_process_itd(EHCIState *ehci,
} }
qemu_sglist_destroy(&ehci->isgl); qemu_sglist_destroy(&ehci->isgl);
if (ret == USB_RET_NAK) { if (ret < 0) {
/* no data for us, so do a zero-length transfer */
ret = 0;
}
if (ret >= 0) {
if (!dir) {
/* OUT */
set_field(&itd->transact[i], len - ret, ITD_XACT_LENGTH);
} else {
/* IN */
set_field(&itd->transact[i], ret, ITD_XACT_LENGTH);
}
} else {
switch (ret) { switch (ret) {
default: default:
fprintf(stderr, "Unexpected iso usb result: %d\n", ret); fprintf(stderr, "Unexpected iso usb result: %d\n", ret);
/* Fall through */ /* Fall through */
case USB_RET_IOERROR:
case USB_RET_NODEV: case USB_RET_NODEV:
/* 3.3.2: XACTERR is only allowed on IN transactions */ /* 3.3.2: XACTERR is only allowed on IN transactions */
if (dir) { if (dir) {
@ -1500,6 +1484,19 @@ static int ehci_process_itd(EHCIState *ehci,
itd->transact[i] |= ITD_XACT_BABBLE; itd->transact[i] |= ITD_XACT_BABBLE;
ehci_record_interrupt(ehci, USBSTS_ERRINT); ehci_record_interrupt(ehci, USBSTS_ERRINT);
break; break;
case USB_RET_NAK:
/* no data for us, so do a zero-length transfer */
ret = 0;
break;
}
}
if (ret >= 0) {
if (!dir) {
/* OUT */
set_field(&itd->transact[i], len - ret, ITD_XACT_LENGTH);
} else {
/* IN */
set_field(&itd->transact[i], ret, ITD_XACT_LENGTH);
} }
} }
if (itd->transact[i] & ITD_XACT_IOC) { if (itd->transact[i] & ITD_XACT_IOC) {
@ -1526,7 +1523,7 @@ static int ehci_state_waitlisthead(EHCIState *ehci, int async)
ehci_set_usbsts(ehci, USBSTS_REC); ehci_set_usbsts(ehci, USBSTS_REC);
} }
ehci_queues_rip_unused(ehci); ehci_queues_rip_unused(ehci, async, 0);
/* Find the head of the list (4.9.1.1) */ /* Find the head of the list (4.9.1.1) */
for(i = 0; i < MAX_QH; i++) { for(i = 0; i < MAX_QH; i++) {
@ -1568,8 +1565,7 @@ static int ehci_state_fetchentry(EHCIState *ehci, int async)
int again = 0; int again = 0;
uint32_t entry = ehci_get_fetch_addr(ehci, async); uint32_t entry = ehci_get_fetch_addr(ehci, async);
if (entry < 0x1000) { if (NLPTR_TBIT(entry)) {
DPRINTF("fetchentry: entry invalid (0x%08x)\n", entry);
ehci_set_state(ehci, async, EST_ACTIVE); ehci_set_state(ehci, async, EST_ACTIVE);
goto out; goto out;
} }
@ -1611,10 +1607,9 @@ static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
{ {
uint32_t entry; uint32_t entry;
EHCIQueue *q; EHCIQueue *q;
int reload;
entry = ehci_get_fetch_addr(ehci, async); entry = ehci_get_fetch_addr(ehci, async);
q = ehci_find_queue_by_qh(ehci, entry); q = ehci_find_queue_by_qh(ehci, entry, async);
if (NULL == q) { if (NULL == q) {
q = ehci_alloc_queue(ehci, async); q = ehci_alloc_queue(ehci, async);
} }
@ -1669,15 +1664,11 @@ static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
} }
#endif #endif
reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
if (reload) {
set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
}
if (q->qh.token & QTD_TOKEN_HALT) { if (q->qh.token & QTD_TOKEN_HALT) {
ehci_set_state(ehci, async, EST_HORIZONTALQH); ehci_set_state(ehci, async, EST_HORIZONTALQH);
} else if ((q->qh.token & QTD_TOKEN_ACTIVE) && (q->qh.current_qtd > 0x1000)) { } else if ((q->qh.token & QTD_TOKEN_ACTIVE) &&
(NLPTR_TBIT(q->qh.current_qtd) == 0)) {
q->qtdaddr = q->qh.current_qtd; q->qtdaddr = q->qh.current_qtd;
ehci_set_state(ehci, async, EST_FETCHQTD); ehci_set_state(ehci, async, EST_FETCHQTD);
@ -1756,7 +1747,6 @@ static int ehci_state_advqueue(EHCIQueue *q, int async)
* want data and alt-next qTD is valid * want data and alt-next qTD is valid
*/ */
if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) && if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
(q->qh.altnext_qtd > 0x1000) &&
(NLPTR_TBIT(q->qh.altnext_qtd) == 0)) { (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
q->qtdaddr = q->qh.altnext_qtd; q->qtdaddr = q->qh.altnext_qtd;
ehci_set_state(q->ehci, async, EST_FETCHQTD); ehci_set_state(q->ehci, async, EST_FETCHQTD);
@ -1764,8 +1754,7 @@ static int ehci_state_advqueue(EHCIQueue *q, int async)
/* /*
* next qTD is valid * next qTD is valid
*/ */
} else if ((q->qh.next_qtd > 0x1000) && } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) {
(NLPTR_TBIT(q->qh.next_qtd) == 0)) {
q->qtdaddr = q->qh.next_qtd; q->qtdaddr = q->qh.next_qtd;
ehci_set_state(q->ehci, async, EST_FETCHQTD); ehci_set_state(q->ehci, async, EST_FETCHQTD);
@ -1834,25 +1823,11 @@ static void ehci_flush_qh(EHCIQueue *q)
static int ehci_state_execute(EHCIQueue *q, int async) static int ehci_state_execute(EHCIQueue *q, int async)
{ {
int again = 0; int again = 0;
int reload, nakcnt;
int smask;
if (ehci_qh_do_overlay(q) != 0) { if (ehci_qh_do_overlay(q) != 0) {
return -1; return -1;
} }
smask = get_field(q->qh.epcap, QH_EPCAP_SMASK);
if (!smask) {
reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
if (reload && !nakcnt) {
ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
again = 1;
goto out;
}
}
// TODO verify enough time remains in the uframe as in 4.4.1.1 // TODO verify enough time remains in the uframe as in 4.4.1.1
// TODO write back ptr to async list when done or out of time // TODO write back ptr to async list when done or out of time
// TODO Windows does not seem to ever set the MULT field // TODO Windows does not seem to ever set the MULT field
@ -1894,7 +1869,6 @@ out:
static int ehci_state_executing(EHCIQueue *q, int async) static int ehci_state_executing(EHCIQueue *q, int async)
{ {
int again = 0; int again = 0;
int reload, nakcnt;
ehci_execute_complete(q); ehci_execute_complete(q);
if (q->usb_status == USB_RET_ASYNC) { if (q->usb_status == USB_RET_ASYNC) {
@ -1914,21 +1888,8 @@ static int ehci_state_executing(EHCIQueue *q, int async)
// counter decrements to 0 // counter decrements to 0
} }
reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
if (reload) {
nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
if (q->usb_status == USB_RET_NAK) {
if (nakcnt) {
nakcnt--;
}
} else {
nakcnt = reload;
}
set_field(&q->qh.altnext_qtd, nakcnt, QH_ALTNEXT_NAKCNT);
}
/* 4.10.5 */ /* 4.10.5 */
if ((q->usb_status == USB_RET_NAK) || (q->qh.token & QTD_TOKEN_ACTIVE)) { if (q->usb_status == USB_RET_NAK) {
ehci_set_state(q->ehci, async, EST_HORIZONTALQH); ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
} else { } else {
ehci_set_state(q->ehci, async, EST_WRITEBACK); ehci_set_state(q->ehci, async, EST_WRITEBACK);
@ -2066,7 +2027,7 @@ static void ehci_advance_state(EHCIState *ehci,
static void ehci_advance_async_state(EHCIState *ehci) static void ehci_advance_async_state(EHCIState *ehci)
{ {
int async = 1; const int async = 1;
switch(ehci_get_state(ehci, async)) { switch(ehci_get_state(ehci, async)) {
case EST_INACTIVE: case EST_INACTIVE:
@ -2079,23 +2040,13 @@ static void ehci_advance_async_state(EHCIState *ehci)
case EST_ACTIVE: case EST_ACTIVE:
if ( !(ehci->usbcmd & USBCMD_ASE)) { if ( !(ehci->usbcmd & USBCMD_ASE)) {
ehci_queues_rip_all(ehci, async);
ehci_clear_usbsts(ehci, USBSTS_ASS); ehci_clear_usbsts(ehci, USBSTS_ASS);
ehci_set_state(ehci, async, EST_INACTIVE); ehci_set_state(ehci, async, EST_INACTIVE);
break; break;
} }
/* If the doorbell is set, the guest wants to make a change to the /* make sure guest has acknowledged the doorbell interrupt */
* schedule. The host controller needs to release cached data.
* (section 4.8.2)
*/
if (ehci->usbcmd & USBCMD_IAAD) {
DPRINTF("ASYNC: doorbell request acknowledged\n");
ehci->usbcmd &= ~USBCMD_IAAD;
ehci_set_interrupt(ehci, USBSTS_IAA);
break;
}
/* make sure guest has acknowledged */
/* TO-DO: is this really needed? */ /* TO-DO: is this really needed? */
if (ehci->usbsts & USBSTS_IAA) { if (ehci->usbsts & USBSTS_IAA) {
DPRINTF("IAA status bit still set.\n"); DPRINTF("IAA status bit still set.\n");
@ -2109,6 +2060,18 @@ static void ehci_advance_async_state(EHCIState *ehci)
ehci_set_state(ehci, async, EST_WAITLISTHEAD); ehci_set_state(ehci, async, EST_WAITLISTHEAD);
ehci_advance_state(ehci, async); ehci_advance_state(ehci, async);
/* If the doorbell is set, the guest wants to make a change to the
* schedule. The host controller needs to release cached data.
* (section 4.8.2)
*/
if (ehci->usbcmd & USBCMD_IAAD) {
/* Remove all unseen qhs from the async qhs queue */
ehci_queues_rip_unused(ehci, async, 1);
DPRINTF("ASYNC: doorbell request acknowledged\n");
ehci->usbcmd &= ~USBCMD_IAAD;
ehci_set_interrupt(ehci, USBSTS_IAA);
}
break; break;
default: default:
@ -2123,7 +2086,7 @@ static void ehci_advance_periodic_state(EHCIState *ehci)
{ {
uint32_t entry; uint32_t entry;
uint32_t list; uint32_t list;
int async = 0; const int async = 0;
// 4.6 // 4.6
@ -2138,6 +2101,7 @@ static void ehci_advance_periodic_state(EHCIState *ehci)
case EST_ACTIVE: case EST_ACTIVE:
if ( !(ehci->frindex & 7) && !(ehci->usbcmd & USBCMD_PSE)) { if ( !(ehci->frindex & 7) && !(ehci->usbcmd & USBCMD_PSE)) {
ehci_queues_rip_all(ehci, async);
ehci_clear_usbsts(ehci, USBSTS_PSS); ehci_clear_usbsts(ehci, USBSTS_PSS);
ehci_set_state(ehci, async, EST_INACTIVE); ehci_set_state(ehci, async, EST_INACTIVE);
break; break;
@ -2158,6 +2122,7 @@ static void ehci_advance_periodic_state(EHCIState *ehci)
ehci_set_fetch_addr(ehci, async,entry); ehci_set_fetch_addr(ehci, async,entry);
ehci_set_state(ehci, async, EST_FETCHENTRY); ehci_set_state(ehci, async, EST_FETCHENTRY);
ehci_advance_state(ehci, async); ehci_advance_state(ehci, async);
ehci_queues_rip_unused(ehci, async, 0);
break; break;
default: default:
@ -2356,7 +2321,8 @@ static int usb_ehci_initfn(PCIDevice *dev)
} }
s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s); s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s);
QTAILQ_INIT(&s->queues); QTAILQ_INIT(&s->aqueues);
QTAILQ_INIT(&s->pqueues);
qemu_register_reset(ehci_reset, s); qemu_register_reset(ehci_reset, s);

View File

@ -837,6 +837,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
OHCI_CC_DATAUNDERRUN); OHCI_CC_DATAUNDERRUN);
} else { } else {
switch (ret) { switch (ret) {
case USB_RET_IOERROR:
case USB_RET_NODEV: case USB_RET_NODEV:
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC, OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
OHCI_CC_DEVICENOTRESPONDING); OHCI_CC_DEVICENOTRESPONDING);
@ -1052,6 +1053,7 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN); OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
} else { } else {
switch (ret) { switch (ret) {
case USB_RET_IOERROR:
case USB_RET_NODEV: case USB_RET_NODEV:
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING); OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
case USB_RET_NAK: case USB_RET_NAK:

View File

@ -765,6 +765,7 @@ out:
break; break;
return 1; return 1;
case USB_RET_IOERROR:
case USB_RET_NODEV: case USB_RET_NODEV:
default: default:
break; break;
@ -950,7 +951,6 @@ static void uhci_fill_queue(UHCIState *s, UHCI_TD *td)
UHCI_TD ptd; UHCI_TD ptd;
int ret; int ret;
fprintf(stderr, "%s: -- %x\n", __func__, token);
while (is_valid(plink)) { while (is_valid(plink)) {
pci_dma_read(&s->dev, plink & ~0xf, &ptd, sizeof(ptd)); pci_dma_read(&s->dev, plink & ~0xf, &ptd, sizeof(ptd));
le32_to_cpus(&ptd.link); le32_to_cpus(&ptd.link);

View File

@ -1470,8 +1470,8 @@ static USBDevice *xhci_find_device(XHCIPort *port, uint8_t addr)
static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer) static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
{ {
XHCITRB *trb_setup, *trb_status; XHCITRB *trb_setup, *trb_status;
uint8_t bmRequestType, bRequest; uint8_t bmRequestType;
uint16_t wValue, wLength, wIndex; uint16_t wLength;
XHCIPort *port; XHCIPort *port;
USBDevice *dev; USBDevice *dev;
int ret; int ret;
@ -1508,9 +1508,6 @@ static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
} }
bmRequestType = trb_setup->parameter; bmRequestType = trb_setup->parameter;
bRequest = trb_setup->parameter >> 8;
wValue = trb_setup->parameter >> 16;
wIndex = trb_setup->parameter >> 32;
wLength = trb_setup->parameter >> 48; wLength = trb_setup->parameter >> 48;
if (xfer->data && xfer->data_alloced < wLength) { if (xfer->data && xfer->data_alloced < wLength) {
@ -1537,12 +1534,12 @@ static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
xfer->iso_xfer = false; xfer->iso_xfer = false;
xhci_setup_packet(xfer, dev); xhci_setup_packet(xfer, dev);
xfer->packet.parameter = trb_setup->parameter;
if (!xfer->in_xfer) { if (!xfer->in_xfer) {
xhci_xfer_data(xfer, xfer->data, wLength, 0, 1, 0); xhci_xfer_data(xfer, xfer->data, wLength, 0, 1, 0);
} }
ret = usb_device_handle_control(dev, &xfer->packet,
(bmRequestType << 8) | bRequest, ret = usb_handle_packet(dev, &xfer->packet);
wValue, wIndex, wLength, xfer->data);
xhci_complete_packet(xfer, ret); xhci_complete_packet(xfer, ret);
if (!xfer->running_async && !xfer->running_retry) { if (!xfer->running_async && !xfer->running_retry) {
@ -2282,7 +2279,7 @@ static void xhci_update_port(XHCIState *xhci, XHCIPort *port, int is_detach)
int nr = port->port.index + 1; int nr = port->port.index + 1;
port->portsc = PORTSC_PP; port->portsc = PORTSC_PP;
if (port->port.dev && !is_detach) { if (port->port.dev && port->port.dev->attached && !is_detach) {
port->portsc |= PORTSC_CCS; port->portsc |= PORTSC_CCS;
switch (port->port.dev->speed) { switch (port->port.dev->speed) {
case USB_SPEED_LOW: case USB_SPEED_LOW:

View File

@ -95,6 +95,7 @@ void usb_wakeup(USBEndpoint *ep)
#define SETUP_STATE_SETUP 1 #define SETUP_STATE_SETUP 1
#define SETUP_STATE_DATA 2 #define SETUP_STATE_DATA 2
#define SETUP_STATE_ACK 3 #define SETUP_STATE_ACK 3
#define SETUP_STATE_PARAM 4
static int do_token_setup(USBDevice *s, USBPacket *p) static int do_token_setup(USBDevice *s, USBPacket *p)
{ {
@ -226,6 +227,50 @@ static int do_token_out(USBDevice *s, USBPacket *p)
} }
} }
static int do_parameter(USBDevice *s, USBPacket *p)
{
int request, value, index;
int i, ret = 0;
for (i = 0; i < 8; i++) {
s->setup_buf[i] = p->parameter >> (i*8);
}
s->setup_state = SETUP_STATE_PARAM;
s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
s->setup_index = 0;
request = (s->setup_buf[0] << 8) | s->setup_buf[1];
value = (s->setup_buf[3] << 8) | s->setup_buf[2];
index = (s->setup_buf[5] << 8) | s->setup_buf[4];
if (s->setup_len > sizeof(s->data_buf)) {
fprintf(stderr,
"usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
s->setup_len, sizeof(s->data_buf));
return USB_RET_STALL;
}
if (p->pid == USB_TOKEN_OUT) {
usb_packet_copy(p, s->data_buf, s->setup_len);
}
ret = usb_device_handle_control(s, p, request, value, index,
s->setup_len, s->data_buf);
if (ret < 0) {
return ret;
}
if (ret < s->setup_len) {
s->setup_len = ret;
}
if (p->pid == USB_TOKEN_IN) {
usb_packet_copy(p, s->data_buf, s->setup_len);
}
return ret;
}
/* ctrl complete function for devices which use usb_generic_handle_packet and /* ctrl complete function for devices which use usb_generic_handle_packet and
may return USB_RET_ASYNC from their handle_control callback. Device code may return USB_RET_ASYNC from their handle_control callback. Device code
which does this *must* call this function instead of the normal which does this *must* call this function instead of the normal
@ -250,6 +295,16 @@ void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
p->result = 0; p->result = 0;
break; break;
case SETUP_STATE_PARAM:
if (p->result < s->setup_len) {
s->setup_len = p->result;
}
if (p->pid == USB_TOKEN_IN) {
p->result = 0;
usb_packet_copy(p, s->data_buf, s->setup_len);
}
break;
default: default:
break; break;
} }
@ -292,6 +347,9 @@ static int usb_process_one(USBPacket *p)
if (p->ep->nr == 0) { if (p->ep->nr == 0) {
/* control pipe */ /* control pipe */
if (p->parameter) {
return do_parameter(dev, p);
}
switch (p->pid) { switch (p->pid) {
case USB_TOKEN_SETUP: case USB_TOKEN_SETUP:
return do_token_setup(dev, p); return do_token_setup(dev, p);
@ -323,7 +381,7 @@ int usb_handle_packet(USBDevice *dev, USBPacket *p)
assert(p->state == USB_PACKET_SETUP); assert(p->state == USB_PACKET_SETUP);
assert(p->ep != NULL); assert(p->ep != NULL);
if (QTAILQ_EMPTY(&p->ep->queue)) { if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline) {
ret = usb_process_one(p); ret = usb_process_one(p);
if (ret == USB_RET_ASYNC) { if (ret == USB_RET_ASYNC) {
usb_packet_set_state(p, USB_PACKET_ASYNC); usb_packet_set_state(p, USB_PACKET_ASYNC);
@ -356,6 +414,9 @@ void usb_packet_complete(USBDevice *dev, USBPacket *p)
while (!QTAILQ_EMPTY(&ep->queue)) { while (!QTAILQ_EMPTY(&ep->queue)) {
p = QTAILQ_FIRST(&ep->queue); p = QTAILQ_FIRST(&ep->queue);
if (p->state == USB_PACKET_ASYNC) {
break;
}
assert(p->state == USB_PACKET_QUEUED); assert(p->state == USB_PACKET_QUEUED);
ret = usb_process_one(p); ret = usb_process_one(p);
if (ret == USB_RET_ASYNC) { if (ret == USB_RET_ASYNC) {
@ -413,6 +474,7 @@ void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
p->pid = pid; p->pid = pid;
p->ep = ep; p->ep = ep;
p->result = 0; p->result = 0;
p->parameter = 0;
qemu_iovec_reset(&p->iov); qemu_iovec_reset(&p->iov);
usb_packet_set_state(p, USB_PACKET_SETUP); usb_packet_set_state(p, USB_PACKET_SETUP);
} }
@ -465,6 +527,7 @@ void usb_ep_init(USBDevice *dev)
dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL; dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
dev->ep_ctl.ifnum = 0; dev->ep_ctl.ifnum = 0;
dev->ep_ctl.dev = dev; dev->ep_ctl.dev = dev;
dev->ep_ctl.pipeline = false;
QTAILQ_INIT(&dev->ep_ctl.queue); QTAILQ_INIT(&dev->ep_ctl.queue);
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
dev->ep_in[ep].nr = ep + 1; dev->ep_in[ep].nr = ep + 1;
@ -477,6 +540,8 @@ void usb_ep_init(USBDevice *dev)
dev->ep_out[ep].ifnum = 0; dev->ep_out[ep].ifnum = 0;
dev->ep_in[ep].dev = dev; dev->ep_in[ep].dev = dev;
dev->ep_out[ep].dev = dev; dev->ep_out[ep].dev = dev;
dev->ep_in[ep].pipeline = false;
dev->ep_out[ep].pipeline = false;
QTAILQ_INIT(&dev->ep_in[ep].queue); QTAILQ_INIT(&dev->ep_in[ep].queue);
QTAILQ_INIT(&dev->ep_out[ep].queue); QTAILQ_INIT(&dev->ep_out[ep].queue);
} }
@ -590,3 +655,9 @@ int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
return uep->max_packet_size; return uep->max_packet_size;
} }
void usb_ep_set_pipeline(USBDevice *dev, int pid, int ep, bool enabled)
{
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
uep->pipeline = enabled;
}

View File

@ -39,11 +39,12 @@
#define USB_TOKEN_IN 0x69 /* device -> host */ #define USB_TOKEN_IN 0x69 /* device -> host */
#define USB_TOKEN_OUT 0xe1 /* host -> device */ #define USB_TOKEN_OUT 0xe1 /* host -> device */
#define USB_RET_NODEV (-1) #define USB_RET_NODEV (-1)
#define USB_RET_NAK (-2) #define USB_RET_NAK (-2)
#define USB_RET_STALL (-3) #define USB_RET_STALL (-3)
#define USB_RET_BABBLE (-4) #define USB_RET_BABBLE (-4)
#define USB_RET_ASYNC (-5) #define USB_RET_IOERROR (-5)
#define USB_RET_ASYNC (-6)
#define USB_SPEED_LOW 0 #define USB_SPEED_LOW 0
#define USB_SPEED_FULL 1 #define USB_SPEED_FULL 1
@ -176,6 +177,7 @@ struct USBEndpoint {
uint8_t type; uint8_t type;
uint8_t ifnum; uint8_t ifnum;
int max_packet_size; int max_packet_size;
bool pipeline;
USBDevice *dev; USBDevice *dev;
QTAILQ_HEAD(, USBPacket) queue; QTAILQ_HEAD(, USBPacket) queue;
}; };
@ -325,6 +327,7 @@ struct USBPacket {
int pid; int pid;
USBEndpoint *ep; USBEndpoint *ep;
QEMUIOVector iov; QEMUIOVector iov;
uint64_t parameter; /* control transfers */
int result; /* transfer length or USB_RET_* status code */ int result; /* transfer length or USB_RET_* status code */
/* Internal use by the USB layer. */ /* Internal use by the USB layer. */
USBPacketState state; USBPacketState state;
@ -363,6 +366,7 @@ void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum);
void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
uint16_t raw); uint16_t raw);
int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep); int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep);
void usb_ep_set_pipeline(USBDevice *dev, int pid, int ep, bool enabled);
void usb_attach(USBPort *port); void usb_attach(USBPort *port);
void usb_detach(USBPort *port); void usb_detach(USBPort *port);

View File

@ -364,8 +364,12 @@ static void async_complete(void *opaque)
p->result = USB_RET_STALL; p->result = USB_RET_STALL;
break; break;
case -EOVERFLOW:
p->result = USB_RET_BABBLE;
break;
default: default:
p->result = USB_RET_NAK; p->result = USB_RET_IOERROR;
break; break;
} }
@ -722,8 +726,10 @@ static int urb_status_to_usb_ret(int status)
switch (status) { switch (status) {
case -EPIPE: case -EPIPE:
return USB_RET_STALL; return USB_RET_STALL;
case -EOVERFLOW:
return USB_RET_BABBLE;
default: default:
return USB_RET_NAK; return USB_RET_IOERROR;
} }
} }
@ -759,7 +765,7 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
} else if (aurb[i].urb.iso_frame_desc[j].actual_length } else if (aurb[i].urb.iso_frame_desc[j].actual_length
> p->iov.size) { > p->iov.size) {
printf("husb: received iso data is larger then packet\n"); printf("husb: received iso data is larger then packet\n");
len = USB_RET_NAK; len = USB_RET_BABBLE;
/* All good copy data over */ /* All good copy data over */
} else { } else {
len = aurb[i].urb.iso_frame_desc[j].actual_length; len = aurb[i].urb.iso_frame_desc[j].actual_length;
@ -1186,6 +1192,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
USB_ENDPOINT_XFER_INVALID); USB_ENDPOINT_XFER_INVALID);
usb_ep_set_type(&s->dev, pid, ep, type); usb_ep_set_type(&s->dev, pid, ep, type);
usb_ep_set_ifnum(&s->dev, pid, ep, interface); usb_ep_set_ifnum(&s->dev, pid, ep, interface);
if (type == USB_ENDPOINT_XFER_BULK) {
usb_ep_set_pipeline(&s->dev, pid, ep, true);
}
epd = get_endp(s, pid, ep); epd = get_endp(s, pid, ep);
epd->halted = 0; epd->halted = 0;

View File

@ -431,7 +431,7 @@ static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
/* Check iso_error for stream errors, otherwise its an underrun */ /* Check iso_error for stream errors, otherwise its an underrun */
status = dev->endpoint[EP2I(ep)].iso_error; status = dev->endpoint[EP2I(ep)].iso_error;
dev->endpoint[EP2I(ep)].iso_error = 0; dev->endpoint[EP2I(ep)].iso_error = 0;
return status ? USB_RET_NAK : 0; return status ? USB_RET_IOERROR : 0;
} }
DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep, DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size); isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
@ -439,7 +439,7 @@ static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
status = isop->status; status = isop->status;
if (status != usb_redir_success) { if (status != usb_redir_success) {
bufp_free(dev, isop, ep); bufp_free(dev, isop, ep);
return USB_RET_NAK; return USB_RET_IOERROR;
} }
len = isop->len; len = isop->len;
@ -447,7 +447,7 @@ static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
ERROR("received iso data is larger then packet ep %02X (%d > %d)\n", ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
ep, len, (int)p->iov.size); ep, len, (int)p->iov.size);
bufp_free(dev, isop, ep); bufp_free(dev, isop, ep);
return USB_RET_NAK; return USB_RET_BABBLE;
} }
usb_packet_copy(p, isop->data, len); usb_packet_copy(p, isop->data, len);
bufp_free(dev, isop, ep); bufp_free(dev, isop, ep);
@ -566,7 +566,7 @@ static int usbredir_handle_interrupt_data(USBRedirDevice *dev,
if (len > p->iov.size) { if (len > p->iov.size) {
ERROR("received int data is larger then packet ep %02X\n", ep); ERROR("received int data is larger then packet ep %02X\n", ep);
bufp_free(dev, intp, ep); bufp_free(dev, intp, ep);
return USB_RET_NAK; return USB_RET_BABBLE;
} }
usb_packet_copy(p, intp->data, len); usb_packet_copy(p, intp->data, len);
bufp_free(dev, intp, ep); bufp_free(dev, intp, ep);
@ -1018,11 +1018,14 @@ static int usbredir_handle_status(USBRedirDevice *dev,
return USB_RET_STALL; return USB_RET_STALL;
case usb_redir_cancelled: case usb_redir_cancelled:
WARNING("returning cancelled packet to HC?\n"); WARNING("returning cancelled packet to HC?\n");
return USB_RET_NAK;
case usb_redir_inval: case usb_redir_inval:
WARNING("got invalid param error from usb-host?\n");
return USB_RET_NAK;
case usb_redir_ioerror: case usb_redir_ioerror:
case usb_redir_timeout: case usb_redir_timeout:
default: default:
return USB_RET_NAK; return USB_RET_IOERROR;
} }
} }
@ -1122,6 +1125,7 @@ static void usbredir_device_disconnect(void *priv)
for (i = 0; i < MAX_ENDPOINTS; i++) { for (i = 0; i < MAX_ENDPOINTS; i++) {
QTAILQ_INIT(&dev->endpoint[i].bufpq); QTAILQ_INIT(&dev->endpoint[i].bufpq);
} }
usb_ep_init(&dev->dev);
dev->interface_info.interface_count = 0; dev->interface_info.interface_count = 0;
} }
@ -1148,6 +1152,7 @@ static void usbredir_ep_info(void *priv,
struct usb_redir_ep_info_header *ep_info) struct usb_redir_ep_info_header *ep_info)
{ {
USBRedirDevice *dev = priv; USBRedirDevice *dev = priv;
struct USBEndpoint *usb_ep;
int i; int i;
for (i = 0; i < MAX_ENDPOINTS; i++) { for (i = 0; i < MAX_ENDPOINTS; i++) {
@ -1172,7 +1177,13 @@ static void usbredir_ep_info(void *priv,
default: default:
ERROR("Received invalid endpoint type\n"); ERROR("Received invalid endpoint type\n");
usbredir_device_disconnect(dev); usbredir_device_disconnect(dev);
return;
} }
usb_ep = usb_ep_get(&dev->dev,
(i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT,
i & 0x0f);
usb_ep->type = dev->endpoint[i].type;
usb_ep->ifnum = dev->endpoint[i].interface;
} }
} }