fc5c27b
>From a6fba82709859fc91e676ce53079eb250bb55750 Mon Sep 17 00:00:00 2001
fc5c27b
From: Hans de Goede <hdegoede@redhat.com>
fc5c27b
Date: Thu, 4 Aug 2011 14:41:07 +0200
fc5c27b
Subject: [PATCH 27/28] usb-redir: Device disconnect + re-connect robustness
fc5c27b
 fixes
fc5c27b
fc5c27b
These fixes mainly target the other side sending some (error status)
fc5c27b
packets after a disconnect packet. In some cases these would get queued
fc5c27b
up and then reported to the controller when a new device gets connected.
fc5c27b
fc5c27b
* Fully reset device state on disconnect
fc5c27b
* Don't allow a connect message when already connected
fc5c27b
* Ignore iso and interrupt status messages when disconnected
fc5c27b
fc5c27b
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
fc5c27b
---
fc5c27b
 usb-redir.c |   22 +++++++++++++++++++++-
fc5c27b
 1 files changed, 21 insertions(+), 1 deletions(-)
fc5c27b
fc5c27b
diff --git a/usb-redir.c b/usb-redir.c
fc5c27b
index 9ce2c8b..6d8f986 100644
fc5c27b
--- a/usb-redir.c
fc5c27b
+++ b/usb-redir.c
fc5c27b
@@ -905,6 +905,11 @@ static void usbredir_device_connect(void *priv,
fc5c27b
 {
fc5c27b
     USBRedirDevice *dev = priv;
fc5c27b
 
fc5c27b
+    if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
fc5c27b
+        ERROR("Received device connect while already connected\n");
fc5c27b
+        return;
fc5c27b
+    }
fc5c27b
+
fc5c27b
     switch (device_connect->speed) {
fc5c27b
     case usb_redir_speed_low:
fc5c27b
         DPRINTF("attaching low speed device\n");
fc5c27b
@@ -933,19 +938,26 @@ static void usbredir_device_connect(void *priv,
fc5c27b
 static void usbredir_device_disconnect(void *priv)
fc5c27b
 {
fc5c27b
     USBRedirDevice *dev = priv;
fc5c27b
+    int i;
fc5c27b
 
fc5c27b
     /* Stop any pending attaches */
fc5c27b
     qemu_del_timer(dev->attach_timer);
fc5c27b
 
fc5c27b
     if (dev->dev.attached) {
fc5c27b
         usb_device_detach(&dev->dev);
fc5c27b
-        usbredir_cleanup_device_queues(dev);
fc5c27b
         /*
fc5c27b
          * Delay next usb device attach to give the guest a chance to see
fc5c27b
          * see the detach / attach in case of quick close / open succession
fc5c27b
          */
fc5c27b
         dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200;
fc5c27b
     }
fc5c27b
+
fc5c27b
+    /* Reset state so that the next dev connected starts with a clean slate */
fc5c27b
+    usbredir_cleanup_device_queues(dev);
fc5c27b
+    memset(dev->endpoint, 0, sizeof(dev->endpoint));
fc5c27b
+    for (i = 0; i < MAX_ENDPOINTS; i++) {
fc5c27b
+        QTAILQ_INIT(&dev->endpoint[i].bufpq);
fc5c27b
+    }
fc5c27b
 }
fc5c27b
 
fc5c27b
 static void usbredir_interface_info(void *priv,
fc5c27b
@@ -1037,6 +1049,10 @@ static void usbredir_iso_stream_status(void *priv, uint32_t id,
fc5c27b
     DPRINTF("iso status %d ep %02X id %u\n", iso_stream_status->status,
fc5c27b
             ep, id);
fc5c27b
 
fc5c27b
+    if (!dev->dev.attached) {
fc5c27b
+        return;
fc5c27b
+    }
fc5c27b
+
fc5c27b
     dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
fc5c27b
     if (iso_stream_status->status == usb_redir_stall) {
fc5c27b
         DPRINTF("iso stream stopped by peer ep %02X\n", ep);
fc5c27b
@@ -1054,6 +1070,10 @@ static void usbredir_interrupt_receiving_status(void *priv, uint32_t id,
fc5c27b
     DPRINTF("interrupt recv status %d ep %02X id %u\n",
fc5c27b
             interrupt_receiving_status->status, ep, id);
fc5c27b
 
fc5c27b
+    if (!dev->dev.attached) {
fc5c27b
+        return;
fc5c27b
+    }
fc5c27b
+
fc5c27b
     dev->endpoint[EP2I(ep)].interrupt_error =
fc5c27b
         interrupt_receiving_status->status;
fc5c27b
     if (interrupt_receiving_status->status == usb_redir_stall) {
fc5c27b
-- 
fc5c27b
1.7.5.1
fc5c27b