From eb2de9d5679029500ff09a4c8a287a203f5e12b9 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Sun, 4 Mar 2012 11:57:56 +0100
Subject: [PATCH 34/35] usb: add USB_RET_IOERROR

RH-Author: Hans de Goede <hdegoede@redhat.com>
Message-id: <1330862278-22314-20-git-send-email-hdegoede@redhat.com>
Patchwork-id: 37934
O-Subject: [PATCH 19/21] usb: add USB_RET_IOERROR
Bugzilla: 758104
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
RH-Acked-by: Alon Levy <alevy@redhat.com>
RH-Acked-by: Markus Armbruster <armbru@redhat.com>

We already have USB_RET_NAK, but that means that a device does not want
to send/receive right now. But with host / network redirection we can
actually have a transaction fail due to some io error, rather then ie
the device just not having any data atm.

This patch adds a new error code named USB_RET_IOERROR for this, and uses
it were appropriate.

Notes:
-Currently all usb-controllers handle this the same as NODEV, but that
 may change in the future, OHCI could indicate a CRC error instead for example.
-This patch does not touch hw/usb-musb.c, that is because the code in there
 handles STALL and NAK specially and has a if status < 0 generic catch all
 for all other errors

Signed-off-by: Hans de Goede <hdegoede@redhat.com>

Upstream commit: d61000a8b1d99c5155440b727ea32f12a4988120
Upstream: http://patchwork.ozlabs.org/patch/144343/
(waiting for next usb pull request)

[ rhel6: resolve conflicts ]
---
 hw/usb-ehci.c |    2 ++
 hw/usb-ohci.c |    2 ++
 hw/usb-uhci.c |    1 +
 hw/usb.h      |   11 ++++++-----
 usb-linux.c   |    4 ++--
 usb-redir.c   |    9 ++++++---
 6 files changed, 19 insertions(+), 10 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/usb-ehci.c |    2 ++
 hw/usb-ohci.c |    2 ++
 hw/usb-uhci.c |    1 +
 hw/usb.h      |   11 ++++++-----
 usb-linux.c   |    4 ++--
 usb-redir.c   |    9 ++++++---
 6 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/hw/usb-ehci.c b/hw/usb-ehci.c
index fa552c7..9df1d03 100644
--- a/hw/usb-ehci.c
+++ b/hw/usb-ehci.c
@@ -1266,6 +1266,7 @@ static void ehci_execute_complete(EHCIQueue *q)
 
     if (q->usb_status < 0) {
         switch(q->usb_status) {
+        case USB_RET_IOERROR:
         case USB_RET_NODEV:
             q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
             set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
@@ -1476,6 +1477,7 @@ static int ehci_process_itd(EHCIState *ehci,
                 default: 
                     fprintf(stderr, "Unexpected iso usb result: %d\n", ret);
                     /* Fall through */
+                case USB_RET_IOERROR:
                 case USB_RET_NODEV:
                     /* 3.3.2: XACTERR is only allowed on IN transactions */
                     if (dir) {
diff --git a/hw/usb-ohci.c b/hw/usb-ohci.c
index 5597b4d..7ba9d1b 100644
--- a/hw/usb-ohci.c
+++ b/hw/usb-ohci.c
@@ -789,6 +789,7 @@ static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
                         OHCI_CC_DATAUNDERRUN);
         } else {
             switch (ret) {
+            case USB_RET_IOERROR:
             case USB_RET_NODEV:
                 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
                             OHCI_CC_DEVICENOTRESPONDING);
@@ -989,6 +990,7 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
             OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
         } else {
             switch (ret) {
+            case USB_RET_IOERROR:
             case USB_RET_NODEV:
                 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
             case USB_RET_NAK:
diff --git a/hw/usb-uhci.c b/hw/usb-uhci.c
index b54d481..f986f83 100644
--- a/hw/usb-uhci.c
+++ b/hw/usb-uhci.c
@@ -743,6 +743,7 @@ out:
             break;
 	return 1;
 
+    case USB_RET_IOERROR:
     case USB_RET_NODEV:
     default:
 	break;
diff --git a/hw/usb.h b/hw/usb.h
index b8f8ffd..ce06aa6 100644
--- a/hw/usb.h
+++ b/hw/usb.h
@@ -35,11 +35,12 @@
 #define USB_MSG_DETACH   0x101
 #define USB_MSG_RESET    0x102
 
-#define USB_RET_NODEV  (-1)
-#define USB_RET_NAK    (-2)
-#define USB_RET_STALL  (-3)
-#define USB_RET_BABBLE (-4)
-#define USB_RET_ASYNC  (-5)
+#define USB_RET_NODEV   (-1)
+#define USB_RET_NAK     (-2)
+#define USB_RET_STALL   (-3)
+#define USB_RET_BABBLE  (-4)
+#define USB_RET_IOERROR (-5)
+#define USB_RET_ASYNC   (-6)
 
 #define USB_SPEED_LOW   0
 #define USB_SPEED_FULL  1
diff --git a/usb-linux.c b/usb-linux.c
index 4723694..f198d2d 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -391,7 +391,7 @@ static void async_complete(void *opaque)
                 break;
 
             default:
-                p->len = USB_RET_NAK;
+                p->len = USB_RET_IOERROR;
                 break;
             }
 
@@ -732,7 +732,7 @@ static int urb_status_to_usb_ret(int status)
     case -EOVERFLOW:
         return USB_RET_BABBLE;
     default:
-        return USB_RET_NAK;
+        return USB_RET_IOERROR;
     }
 }
 
diff --git a/usb-redir.c b/usb-redir.c
index cef6979..8349416 100644
--- a/usb-redir.c
+++ b/usb-redir.c
@@ -441,7 +441,7 @@ static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
             /* Check iso_error for stream errors, otherwise its an underrun */
             status = dev->endpoint[EP2I(ep)].iso_error;
             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,
                  isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
@@ -449,7 +449,7 @@ static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
         status = isop->status;
         if (status != usb_redir_success) {
             bufp_free(dev, isop, ep);
-            return USB_RET_NAK;
+            return USB_RET_IOERROR;
         }
 
         len = isop->len;
@@ -1040,11 +1040,14 @@ static int usbredir_handle_status(USBRedirDevice *dev,
         return USB_RET_STALL;
     case usb_redir_cancelled:
         WARNING("returning cancelled packet to HC?\n");
+        return USB_RET_NAK;
     case usb_redir_inval:
+        WARNING("got invalid param error from usb-host?\n");
+        return USB_RET_NAK;
     case usb_redir_ioerror:
     case usb_redir_timeout:
     default:
-        return USB_RET_NAK;
+        return USB_RET_IOERROR;
     }
 }
 
-- 
1.7.7.6

