From 1dc297bcb7aa8724f5115f44e391085ce543d79a Mon Sep 17 00:00:00 2001
Message-Id: <1dc297bcb7aa8724f5115f44e391085ce543d79a.1353591647.git.minovotn@redhat.com>
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Fri, 16 Nov 2012 14:53:51 +0100
Subject: [PATCH 1/3] usb-host: scan for usb devices when the vm starts

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1353077631-7335-1-git-send-email-kraxel@redhat.com>
Patchwork-id: 44258
O-Subject: [RHEL-6.4 qemu-kvm PATCH] usb-host: scan for usb devices when the vm starts
Bugzilla: 876534
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Vlad Yasevich <vyasevic@redhat.com>
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>

Commit a844ed842d9a9d929645c09ae0f52f753d7a02e0 leads to usb-host
detecting devices not right after qemu startup because the guest
isn't running yet.  Instead they are found on the first of the
regular usb device poll runs.  Which is too late for seabios to see
them, so booting from usb sticks fails.

Fix this by adding a vm state change handler which triggers a device
scan when the vm is started.

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

bugzilla: #876534 - [regression] unable to boot from usb-host devices
upstream: pull request sent
---
 usb-linux.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 usb-linux.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/usb-linux.c b/usb-linux.c
index cd1665b..2a411ac 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -1857,6 +1857,7 @@ static int usb_host_scan(void *opaque, USBScanFunc *func)
 }
 
 static QEMUTimer *usb_auto_timer;
+static VMChangeStateEntry *usb_vmstate;
 
 static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
                               int class_id, int vendor_id, int product_id,
@@ -1907,6 +1908,13 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
     return 0;
 }
 
+static void usb_host_vm_state(void *unused, int running, RunState state)
+{
+    if (running) {
+        usb_host_auto_check(unused);
+    }
+}
+
 static void usb_host_auto_check(void *unused)
 {
     struct USBHostDevice *s;
@@ -1934,6 +1942,9 @@ static void usb_host_auto_check(void *unused)
         }
     }
 
+    if (!usb_vmstate) {
+        usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
+    }
     if (!usb_auto_timer) {
         usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
         if (!usb_auto_timer)
-- 
1.7.11.7

