qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Krasnyansky <maxk@kernel.org>
To: qemu-devel@nongnu.org
Cc: kvm@vger.kernel.org, Max Krasnyansky <maxk@kernel.org>
Subject: [Qemu-devel] [PATCH 1/5] husb: support for USB host device auto disconnect.
Date: Thu, 14 Aug 2008 04:22:09 +0000	[thread overview]
Message-ID: <75c6e94e6d567896941e879ef7eddb29ecc3f6fa.1218685608.git.maxk@kernel.org> (raw)
In-Reply-To: <cover.1218685607.git.maxk@kernel.org>
In-Reply-To: <cover.1218685607.git.maxk@kernel.org>

I got really annoyed by the fact that you have to manually do
usb_del in the monitor when host device is unplugged and decided
to fix it :)

Basically we now automatically remove guest USB device
when the actual host device is disconnected.

At first I've extended set_fd_handlerX() stuff to support checking
for exceptions on fds. But unfortunately usbfs code does not wake up
user-space process when device is removed, which means we need a
timer to periodically check if device is still there. So I removed
fd exception stuff and implemented it with the timer.

Signed-off-by: Max Krasnyansky <maxk@kernel.org>
---
 hw/usb.h    |    1 +
 usb-linux.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++---------
 vl.c        |   26 ++++++++++++++++++--------
 3 files changed, 66 insertions(+), 17 deletions(-)

diff --git a/hw/usb.h b/hw/usb.h
index 8bdc68d..2edb982 100644
--- a/hw/usb.h
+++ b/hw/usb.h
@@ -197,6 +197,7 @@ static inline void usb_cancel_packet(USBPacket * p)
     p->cancel_cb(p, p->cancel_opaque);
 }
 
+int usb_device_del_addr(int bus_num, int addr);
 void usb_attach(USBPort *port, USBDevice *dev);
 int usb_generic_handle_packet(USBDevice *s, USBPacket *p);
 int set_usb_string(uint8_t *buf, const char *str);
diff --git a/usb-linux.c b/usb-linux.c
index d3e4e2e..0023c1d 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
+#include "qemu-timer.h"
 #include "hw/usb.h"
 #include "console.h"
 
@@ -77,6 +78,7 @@ typedef struct USBHostDevice {
     uint8_t descr[1024];
     int descr_len;
     int urbs_ready;
+    QEMUTimer *timer;
 } USBHostDevice;
 
 typedef struct PendingURB {
@@ -165,7 +167,11 @@ static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
         }
         config_descr_len = dev->descr[i];
 
-        if (configuration == dev->descr[i + 5])
+#ifdef DEBUG
+	printf("config #%d need %d\n", dev->descr[i + 5], configuration); 
+#endif
+
+        if (configuration < 0 || configuration == dev->descr[i + 5])
             break;
 
         i += config_descr_len;
@@ -230,8 +236,11 @@ static void usb_host_handle_destroy(USBDevice *dev)
 {
     USBHostDevice *s = (USBHostDevice *)dev;
 
+    qemu_del_timer(s->timer);
+
     if (s->fd >= 0)
         close(s->fd);
+
     qemu_free(s);
 }
 
@@ -594,6 +603,22 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
     return 0;
 }
 
+static void usb_host_device_check(void *priv)
+{
+    USBHostDevice *s = priv;
+    struct usbdevfs_connectinfo ci;
+    int err;
+
+    err = ioctl(s->fd, USBDEVFS_CONNECTINFO, &ci);
+    if (err < 0) {
+        printf("usb device %d.%d disconnected\n", 0, s->dev.addr);
+	usb_device_del_addr(0, s->dev.addr);
+	return;
+    }
+
+    qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
+}
+
 /* XXX: exclude high speed devices or implement EHCI */
 USBDevice *usb_host_device_open(const char *devname)
 {
@@ -604,24 +629,30 @@ USBDevice *usb_host_device_open(const char *devname)
     int bus_num, addr;
     char product_name[PRODUCT_NAME_SZ];
 
+    if (usb_host_find_device(&bus_num, &addr,
+                             product_name, sizeof(product_name),
+                             devname) < 0)
+        return NULL;
+
+
     dev = qemu_mallocz(sizeof(USBHostDevice));
     if (!dev)
         goto fail;
 
-#ifdef DEBUG_ISOCH
+    dev->timer = qemu_new_timer(rt_clock, usb_host_device_check, (void *) dev);
+    if (!dev->timer)
+	goto fail;
+
+#ifdef DEBUG
     printf("usb_host_device_open %s\n", devname);
 #endif
-    if (usb_host_find_device(&bus_num, &addr,
-                             product_name, sizeof(product_name),
-                             devname) < 0)
-        return NULL;
 
     snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
              bus_num, addr);
     fd = open(buf, O_RDWR | O_NONBLOCK);
     if (fd < 0) {
         perror(buf);
-        return NULL;
+        goto fail;
     }
 
     /* read the device description */
@@ -645,7 +676,7 @@ USBDevice *usb_host_device_open(const char *devname)
     dev->configuration = 1;
 
     /* XXX - do something about initial configuration */
-    if (!usb_host_update_interfaces(dev, 1))
+    if (!usb_host_update_interfaces(dev, -1))
         goto fail;
 
     ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
@@ -700,11 +731,18 @@ USBDevice *usb_host_device_open(const char *devname)
     fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK);
     qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev);
 #endif
+
+    /* Start the timer to detect disconnect */
+    qemu_mod_timer(dev->timer, qemu_get_clock(rt_clock) + 1000);
+
     dev->urbs_ready = 0;
     return (USBDevice *)dev;
 fail:
-    if (dev)
+    if (dev) {
+	if (dev->timer)
+		qemu_del_timer(dev->timer);
         qemu_free(dev);
+    }
     close(fd);
     return NULL;
 }
diff --git a/vl.c b/vl.c
index 6ade2ad..a278599 100644
--- a/vl.c
+++ b/vl.c
@@ -5805,22 +5805,15 @@ static int usb_device_add(const char *devname)
     return 0;
 }
 
-static int usb_device_del(const char *devname)
+int usb_device_del_addr(int bus_num, int addr)
 {
     USBPort *port;
     USBPort **lastp;
     USBDevice *dev;
-    int bus_num, addr;
-    const char *p;
 
     if (!used_usb_ports)
         return -1;
 
-    p = strchr(devname, '.');
-    if (!p)
-        return -1;
-    bus_num = strtoul(devname, NULL, 0);
-    addr = strtoul(p + 1, NULL, 0);
     if (bus_num != 0)
         return -1;
 
@@ -5843,6 +5836,23 @@ static int usb_device_del(const char *devname)
     return 0;
 }
 
+static int usb_device_del(const char *devname)
+{
+    int bus_num, addr;
+    const char *p;
+
+    if (!used_usb_ports)
+        return -1;
+
+    p = strchr(devname, '.');
+    if (!p)
+        return -1;
+    bus_num = strtoul(devname, NULL, 0);
+    addr = strtoul(p + 1, NULL, 0);
+
+    return usb_device_del_addr(bus_num, addr);
+}
+
 void do_usb_add(const char *devname)
 {
     int ret;
-- 
1.5.5.1

  reply	other threads:[~2008-08-14  4:22 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-14  4:22 [Qemu-devel] [PATCH 0/5] Various USB fixes and improvements Max Krasnyansky
2008-08-14  4:22 ` Max Krasnyansky [this message]
2008-08-14 16:28   ` [Qemu-devel] Re: [PATCH 1/5] husb: support for USB host device auto disconnect Anthony Liguori
2008-08-14 19:26     ` Max Krasnyansky
2008-08-14 21:41     ` Max Krasnyansky
2008-08-14  4:22 ` [Qemu-devel] [PATCH 2/5] husb: support for USB host device auto connect Max Krasnyansky
2008-08-14 16:41   ` [Qemu-devel] " Anthony Liguori
2008-08-14 19:38     ` Max Krasnyansky
2008-08-14 20:21       ` Anthony Liguori
2008-08-14 20:34         ` Max Krasnyansky
2008-08-14 20:41           ` Anthony Liguori
2008-08-14 21:14             ` François Revol
2008-08-15  7:46             ` Guido Günther
2008-08-15 18:24               ` Max Krasnyansky
2008-08-15 18:31                 ` Javier Guerra
2008-08-18 18:21                   ` Max Krasnyansky
2008-08-18 18:52                     ` Javier Guerra
2008-08-18 18:56                       ` Jamie Lokier
2008-08-17  7:52                 ` Avi Kivity
2008-08-18 18:46                   ` Max Krasnyansky
2008-08-18 14:11                 ` Anthony Liguori
2008-08-18 18:16                   ` Max Krasnyansky
2008-08-14  4:22 ` [Qemu-devel] [PATCH 3/5] usb: generic packet handler cleanup and documentation Max Krasnyansky
2008-08-14  4:22 ` [Qemu-devel] [PATCH 4/5] uhci: rewrite UHCI emulator, fully async operation with multiple outstanding transactions Max Krasnyansky
2008-08-14 17:51   ` [Qemu-devel] " Anthony Liguori
2008-08-14 19:49     ` Max Krasnyansky
2008-10-11 23:54   ` [Qemu-devel] " Juergen Lock
2008-10-15 19:54     ` Max Krasnyansky
2008-10-15 22:05       ` andrzej zaborowski
2008-10-16 21:25         ` Juergen Lock
2008-08-14  4:22 ` [Qemu-devel] [PATCH 5/5] husb: rewrite Linux host USB layer, fully async operation Max Krasnyansky
2008-08-15 14:24   ` Paul Brook
2008-08-15 19:04     ` Max Krasnyansky
2008-08-15 19:53       ` Paul Brook
2008-08-18 18:40         ` Max Krasnyansky
2008-08-14 17:55 ` [Qemu-devel] Re: [PATCH 0/5] Various USB fixes and improvements Anthony Liguori
2008-08-14 19:55   ` Max Krasnyansky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75c6e94e6d567896941e879ef7eddb29ecc3f6fa.1218685608.git.maxk@kernel.org \
    --to=maxk@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).