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

* kraxel/usb.72:
  usb-redir: Don't handle interrupt output packets async
  usb-redir: Split usb_handle_interrupt_data into separate in/out functions
  usb-smartcard-reader: Properly NAK interrupt eps when we've no events
  usb-bt: Return NAK instead of STALL when interrupt ep has no data
  uhci: Fix double unlink
  uhci: Don't allow the guest to set port-enabled when there is no dev connected
  uhci: Add a completions_only flag for async completions

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Anthony Liguori 2012-11-26 08:19:20 -06:00
commit 95a6e48d77
4 changed files with 121 additions and 85 deletions

View File

@ -27,6 +27,7 @@
struct USBBtState { struct USBBtState {
USBDevice dev; USBDevice dev;
struct HCIInfo *hci; struct HCIInfo *hci;
USBEndpoint *intr;
int config; int config;
@ -290,10 +291,7 @@ static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
{ {
int len; int len;
if (likely(!fifo->len)) { assert(fifo->len != 0);
p->status = USB_RET_STALL;
return;
}
len = MIN(p->iov.size, fifo->fifo[fifo->start].len); len = MIN(p->iov.size, fifo->fifo[fifo->start].len);
usb_packet_copy(p, fifo->fifo[fifo->start].data, len); usb_packet_copy(p, fifo->fifo[fifo->start].data, len);
@ -422,14 +420,26 @@ static void usb_bt_handle_data(USBDevice *dev, USBPacket *p)
case USB_TOKEN_IN: case USB_TOKEN_IN:
switch (p->ep->nr) { switch (p->ep->nr) {
case USB_EVT_EP: case USB_EVT_EP:
if (s->evt.len == 0) {
p->status = USB_RET_NAK;
break;
}
usb_bt_fifo_dequeue(&s->evt, p); usb_bt_fifo_dequeue(&s->evt, p);
break; break;
case USB_ACL_EP: case USB_ACL_EP:
if (s->evt.len == 0) {
p->status = USB_RET_STALL;
break;
}
usb_bt_fifo_dequeue(&s->acl, p); usb_bt_fifo_dequeue(&s->acl, p);
break; break;
case USB_SCO_EP: case USB_SCO_EP:
if (s->evt.len == 0) {
p->status = USB_RET_STALL;
break;
}
usb_bt_fifo_dequeue(&s->sco, p); usb_bt_fifo_dequeue(&s->sco, p);
break; break;
@ -467,6 +477,9 @@ static void usb_bt_out_hci_packet_event(void *opaque,
{ {
struct USBBtState *s = (struct USBBtState *) opaque; struct USBBtState *s = (struct USBBtState *) opaque;
if (s->evt.len == 0) {
usb_wakeup(s->intr);
}
usb_bt_fifo_enqueue(&s->evt, data, len); usb_bt_fifo_enqueue(&s->evt, data, len);
} }
@ -489,8 +502,12 @@ static void usb_bt_handle_destroy(USBDevice *dev)
static int usb_bt_initfn(USBDevice *dev) static int usb_bt_initfn(USBDevice *dev)
{ {
struct USBBtState *s = DO_UPCAST(struct USBBtState, dev, dev);
usb_desc_create_serial(dev); usb_desc_create_serial(dev);
usb_desc_init(dev); usb_desc_init(dev);
s->intr = usb_ep_get(dev, USB_TOKEN_IN, USB_EVT_EP);
return 0; return 0;
} }

View File

@ -1002,6 +1002,8 @@ static void ccid_handle_data(USBDevice *dev, USBPacket *p)
"handle_data: int_in: notify_slot_change %X, " "handle_data: int_in: notify_slot_change %X, "
"requested len %zd\n", "requested len %zd\n",
s->bmSlotICCState, p->iov.size); s->bmSlotICCState, p->iov.size);
} else {
p->status = USB_RET_NAK;
} }
break; break;
default: default:

View File

@ -152,6 +152,7 @@ struct UHCIState {
QEMUBH *bh; QEMUBH *bh;
uint32_t frame_bytes; uint32_t frame_bytes;
uint32_t frame_bandwidth; uint32_t frame_bandwidth;
bool completions_only;
UHCIPort ports[NB_PORTS]; UHCIPort ports[NB_PORTS];
/* Interrupts that should be raised at the end of the current frame. */ /* Interrupts that should be raised at the end of the current frame. */
@ -555,6 +556,10 @@ static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
} }
} }
port->ctrl &= UHCI_PORT_READ_ONLY; port->ctrl &= UHCI_PORT_READ_ONLY;
/* enabled may only be set if a device is connected */
if (!(port->ctrl & UHCI_PORT_CCS)) {
val &= ~UHCI_PORT_EN;
}
port->ctrl |= (val & ~UHCI_PORT_READ_ONLY); port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
/* some bits are reset when a '1' is written to them */ /* some bits are reset when a '1' is written to them */
port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR); port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
@ -891,6 +896,10 @@ static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
goto done; goto done;
} }
if (s->completions_only) {
return TD_RESULT_ASYNC_CONT;
}
/* Allocate new packet */ /* Allocate new packet */
if (q == NULL) { if (q == NULL) {
USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f); USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
@ -954,16 +963,15 @@ static void uhci_async_complete(USBPort *port, USBPacket *packet)
UHCIState *s = async->queue->uhci; UHCIState *s = async->queue->uhci;
if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
uhci_async_unlink(async);
uhci_async_cancel(async); uhci_async_cancel(async);
return; return;
} }
async->done = 1; async->done = 1;
if (s->frame_bytes < s->frame_bandwidth) { /* Force processing of this packet *now*, needed for migration */
s->completions_only = true;
qemu_bh_schedule(s->bh); qemu_bh_schedule(s->bh);
} }
}
static int is_valid(uint32_t link) static int is_valid(uint32_t link)
{ {
@ -1054,7 +1062,7 @@ static void uhci_process_frame(UHCIState *s)
qhdb_reset(&qhdb); qhdb_reset(&qhdb);
for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) { for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
if (s->frame_bytes >= s->frame_bandwidth) { if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
/* We've reached the usb 1.1 bandwidth, which is /* We've reached the usb 1.1 bandwidth, which is
1280 bytes/frame, stop processing */ 1280 bytes/frame, stop processing */
trace_usb_uhci_frame_stop_bandwidth(); trace_usb_uhci_frame_stop_bandwidth();
@ -1170,6 +1178,7 @@ static void uhci_frame_timer(void *opaque)
/* prepare the timer for the next frame */ /* prepare the timer for the next frame */
s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ); s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
s->frame_bytes = 0; s->frame_bytes = 0;
s->completions_only = false;
qemu_bh_cancel(s->bh); qemu_bh_cancel(s->bh);
if (!(s->cmd & UHCI_CMD_RS)) { if (!(s->cmd & UHCI_CMD_RS)) {

View File

@ -610,10 +610,9 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
p->status = USB_RET_ASYNC; p->status = USB_RET_ASYNC;
} }
static void usbredir_handle_interrupt_data(USBRedirDevice *dev, static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev,
USBPacket *p, uint8_t ep) USBPacket *p, uint8_t ep)
{ {
if (ep & USB_DIR_IN) {
/* Input interrupt endpoint, buffered packet input */ /* Input interrupt endpoint, buffered packet input */
struct buf_packet *intp; struct buf_packet *intp;
int status, len; int status, len;
@ -661,19 +660,24 @@ static void usbredir_handle_interrupt_data(USBRedirDevice *dev,
usb_packet_copy(p, intp->data, len); usb_packet_copy(p, intp->data, len);
bufp_free(dev, intp, ep); bufp_free(dev, intp, ep);
usbredir_handle_status(dev, p, status); usbredir_handle_status(dev, p, status);
} else { }
/* Output interrupt endpoint, normal async operation */
/*
* Handle interrupt out data, the usbredir protocol expects us to do this
* async, so that it can report back a completion status. But guests will
* expect immediate completion for an interrupt endpoint, and handling this
* async causes migration issues. So we report success directly, counting
* on the fact that output interrupt packets normally always succeed.
*/
static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
USBPacket *p, uint8_t ep)
{
struct usb_redir_interrupt_packet_header interrupt_packet; struct usb_redir_interrupt_packet_header interrupt_packet;
uint8_t buf[p->iov.size]; uint8_t buf[p->iov.size];
DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep, DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
p->iov.size, p->id); p->iov.size, p->id);
if (usbredir_already_in_flight(dev, p->id)) {
p->status = USB_RET_ASYNC;
return;
}
interrupt_packet.endpoint = ep; interrupt_packet.endpoint = ep;
interrupt_packet.length = p->iov.size; interrupt_packet.length = p->iov.size;
@ -682,8 +686,6 @@ static void usbredir_handle_interrupt_data(USBRedirDevice *dev,
usbredirparser_send_interrupt_packet(dev->parser, p->id, usbredirparser_send_interrupt_packet(dev->parser, p->id,
&interrupt_packet, buf, p->iov.size); &interrupt_packet, buf, p->iov.size);
usbredirparser_do_write(dev->parser); usbredirparser_do_write(dev->parser);
p->status = USB_RET_ASYNC;
}
} }
static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev, static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
@ -729,7 +731,11 @@ static void usbredir_handle_data(USBDevice *udev, USBPacket *p)
usbredir_handle_bulk_data(dev, p, ep); usbredir_handle_bulk_data(dev, p, ep);
break; break;
case USB_ENDPOINT_XFER_INT: case USB_ENDPOINT_XFER_INT:
usbredir_handle_interrupt_data(dev, p, ep); if (ep & USB_DIR_IN) {
usbredir_handle_interrupt_in_data(dev, p, ep);
} else {
usbredir_handle_interrupt_out_data(dev, p, ep);
}
break; break;
default: default:
ERROR("handle_data ep %02X has unknown type %d\n", ep, ERROR("handle_data ep %02X has unknown type %d\n", ep,
@ -1641,11 +1647,13 @@ static void usbredir_interrupt_packet(void *priv, uint64_t id,
/* bufp_alloc also adds the packet to the ep queue */ /* bufp_alloc also adds the packet to the ep queue */
bufp_alloc(dev, data, data_len, interrupt_packet->status, ep); bufp_alloc(dev, data, data_len, interrupt_packet->status, ep);
} else { } else {
USBPacket *p = usbredir_find_packet_by_id(dev, ep, id); /*
if (p) { * We report output interrupt packets as completed directly upon
usbredir_handle_status(dev, p, interrupt_packet->status); * submission, so all we can do here if one failed is warn.
p->actual_length = interrupt_packet->length; */
usb_packet_complete(&dev->dev, p); if (interrupt_packet->status) {
WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n",
interrupt_packet->status, ep, id);
} }
} }
} }