From 63f2310d35c13e15f8d2fe80022f06e128255289 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Fri, 16 Sep 2011 11:17:21 +0200
Subject: [PATCH 4/9] usb-host: limit open retries

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1316171846-30450-5-git-send-email-kraxel@redhat.com>
Patchwork-id: 32850
O-Subject: [RHEL-6.2 kvm PATCH 4/9] usb-host: limit open retries
Bugzilla: 733272
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
RH-Acked-by: Paul Moore <pmoore@redhat.com>

Limit the number of times qemu tries to open host devices to three.
Reset error counter when the device goes away, after un-plugging and
re-plugging the device qemu will try again three times.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
(cherry picked from commit 3ee886c5ba77a65d6b2c2a372a091d6796ed502b)

Conflicts:

	usb-linux.c
---
 usb-linux.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 usb-linux.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/usb-linux.c b/usb-linux.c
index 5e66760..d609aa0 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -132,6 +132,7 @@ typedef struct USBHostDevice {
     int addr;
     char port[MAX_PORTLEN];
     struct USBAutoFilter match;
+    int seen, errcount;
 
     QTAILQ_ENTRY(USBHostDevice) next;
 } USBHostDevice;
@@ -1713,12 +1714,18 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
             continue;
 
         /* We got a match */
+        s->seen++;
+        if (s->errcount >= 3) {
+            return 0;
+        }
 
         /* Already attached ? */
         if (s->fd != -1)
             return 0;
 
-        usb_host_open(s, bus_num, addr, port, product_name, speed);
+        if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
+            s->errcount++;
+        }
         break;
     }
 
@@ -1733,8 +1740,13 @@ static void usb_host_auto_check(void *unused)
     usb_host_scan(NULL, usb_host_auto_scan);
 
     QTAILQ_FOREACH(s, &hostdevs, next) {
-        if (s->fd == -1)
+        if (s->fd == -1) {
             unconnected++;
+        }
+        if (s->seen == 0) {
+            s->errcount = 0;
+        }
+        s->seen = 0;
     }
 
     if (unconnected == 0) {
-- 
1.7.4.4

