Skip to contentSkip to conversation

monitor: add usb_detach

PATCH In review Alon Levy 4 participants 6 messages Oct 5, 2010 – Oct 11, 2010

lore
Share & tools
lore

Alon Levy

author
#0 lore raw

PATCH 1/1 monitor: add usb_detach

---
 qemu-monitor.hx |   17 +++++++++++++++++
 sysemu.h        |    1 +
 vl.c            |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 0 deletions(-)
qemu-monitor.hx +17 -0
index 49bcd8d..9c792a9 100644
@@ -711,6 +711,23 @@ command @code{info usb} to see the devices you can remove.
 ETEXI
 
     {
+        .name       = "usb_detach",
+        .args_type  = "devname:s",
+        .params     = "device",
+        .help       = "detach USB device 'bus.addr'",
+        .mhandler.cmd = do_usb_detach,
+    },
+
+STEXI
+@item usb_detach @var{devname}
+@findex usb_detach
+
+Detach the USB device @var{devname} from the QEMU virtual USB
+hub. @var{devname} has the syntax @code{bus.addr}. Use the monitor
+command @code{info usb} to see the devices you can detach.
+ETEXI
+
+    {
         .name       = "device_add",
         .args_type  = "device:O",
         .params     = "driver[,prop=value][,...]",
sysemu.h +1 -0
index a1f6466..ac68863 100644
@@ -183,6 +183,7 @@ extern struct soundhw soundhw[];
 
 void do_usb_add(Monitor *mon, const QDict *qdict);
 void do_usb_del(Monitor *mon, const QDict *qdict);
+void do_usb_detach(Monitor *mon, const QDict *qdict);
 void usb_info(Monitor *mon);
 
 void rtc_change_mon_event(struct tm *tm);
vl.c +41 -0

47 lines not on this page · raw message

Signature · 6 lines
1.7.3.1



-- 
Alon Levy <[email protected]>
#1 lore raw
Quoted 1 line · On Tue, 5 Oct 2010 16:40:29 +0200 Alon Levy <[email protected]> wrote:

This needs to be rebased as it now conflicts with my last series merged on master.

I have some minor comments (below), but in general seems ok to me. However, I'd like to get an ACK from someone familiar with QEMU's USB before applying to the monitor's queue.

Anthony? Gerd?

Quoted 61 lines
---
 qemu-monitor.hx |   17 +++++++++++++++++
 sysemu.h        |    1 +
 vl.c            |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 0 deletions(-)
qemu-monitor.hx +17 -0
index 49bcd8d..9c792a9 100644
@@ -711,6 +711,23 @@ command @code{info usb} to see the devices you can remove.
 ETEXI
 
     {
+        .name       = "usb_detach",
+        .args_type  = "devname:s",
+        .params     = "device",
+        .help       = "detach USB device 'bus.addr'",
+        .mhandler.cmd = do_usb_detach,
+    },
+
+STEXI
+@item usb_detach @var{devname}
+@findex usb_detach
+
+Detach the USB device @var{devname} from the QEMU virtual USB
+hub. @var{devname} has the syntax @code{bus.addr}. Use the monitor
+command @code{info usb} to see the devices you can detach.
+ETEXI
+
+    {
         .name       = "device_add",
         .args_type  = "device:O",
         .params     = "driver[,prop=value][,...]",
sysemu.h +1 -0
index a1f6466..ac68863 100644
@@ -183,6 +183,7 @@ extern struct soundhw soundhw[];
 
 void do_usb_add(Monitor *mon, const QDict *qdict);
 void do_usb_del(Monitor *mon, const QDict *qdict);
+void do_usb_detach(Monitor *mon, const QDict *qdict);
 void usb_info(Monitor *mon);
 
 void rtc_change_mon_event(struct tm *tm);
vl.c +6 -0
index d352d18..6cfa009 100644
@@ -891,6 +891,47 @@ void do_usb_del(Monitor *mon, const QDict *qdict)
     }
 }
 
+static USBDevice *usb_device_from_bus_dot_addr(const char *devname)
+{
+    int bus_num, addr;
+    const char *p;
+    USBBus *bus;
+    USBPort *port;

vl.c void do_usb_del(Monitor *mon, const QDict *qdict) · line 909

Quoted 10 lines
vl.c +10 -0
+
+    if (!usb_enabled) {
+        return NULL;
+    }
+    p = strchr(devname, '.');
+    if (!p) {
+        return NULL;
+    }
+    bus_num = strtoul(devname, NULL, 0);
+    addr = strtoul(p + 1, NULL, 0);

Checking stroul()'s return would avoid nasty surprises.

Quoted 13 lines
vl.c +13 -0
+    bus = usb_bus_find(bus_num);
+    if (!bus) {
+        return NULL;
+    }
+    QTAILQ_FOREACH(port, &bus->used, next) {
+        if (port->dev->addr == addr) {
+            break;
+        }
+    }
+    if (!port) {
+        return NULL;
+    }
+    return port->dev;

vl.c void do_usb_del(Monitor *mon, const QDict *qdict) · line 932

Quoted 10 lines
vl.c +10 -0
+}
+
+void do_usb_detach(Monitor *mon, const QDict *qdict)
+{
+    const char *devname = qdict_get_str(qdict, "devname");
+    USBDevice *dev = usb_device_from_bus_dot_addr(devname);
+
+    if (dev == NULL || usb_device_detach(dev) < 0) {
+        error_report("could not detach USB device '%s'", devname);
+    }

You can improve the error message if you check for dev and detach separately. Minor.

Quoted 5 lines
vl.c +2 -0
+}
+
 /***********************************************************/
 /* PCMCIA/Cardbus */
 
#2 lore raw
In reply to Alon Levy

How is this different than usb_del? Is it that it detaches it but does not delete the device?

Regards,

Anthony Liguori

Quoted 101 lines · Alon Levy
---
  qemu-monitor.hx |   17 +++++++++++++++++
  sysemu.h        |    1 +
  vl.c            |   41 +++++++++++++++++++++++++++++++++++++++++
  3 files changed, 59 insertions(+), 0 deletions(-)
qemu-monitor.hx +17 -0
index 49bcd8d..9c792a9 100644
@@ -711,6 +711,23 @@ command @code{info usb} to see the devices you can remove.
  ETEXI
 
      {
+        .name       = "usb_detach",
+        .args_type  = "devname:s",
+        .params     = "device",
+        .help       = "detach USB device 'bus.addr'",
+        .mhandler.cmd = do_usb_detach,
+    },
+
+STEXI
+@item usb_detach @var{devname}
+@findex usb_detach
+
+Detach the USB device @var{devname} from the QEMU virtual USB
+hub. @var{devname} has the syntax @code{bus.addr}. Use the monitor
+command @code{info usb} to see the devices you can detach.
+ETEXI
+
+    {
          .name       = "device_add",
          .args_type  = "device:O",
          .params     = "driver[,prop=value][,...]",
sysemu.h +1 -0
index a1f6466..ac68863 100644
@@ -183,6 +183,7 @@ extern struct soundhw soundhw[];
 
  void do_usb_add(Monitor *mon, const QDict *qdict);
  void do_usb_del(Monitor *mon, const QDict *qdict);
+void do_usb_detach(Monitor *mon, const QDict *qdict);
  void usb_info(Monitor *mon);
 
  void rtc_change_mon_event(struct tm *tm);
vl.c +41 -0
index d352d18..6cfa009 100644
@@ -891,6 +891,47 @@ void do_usb_del(Monitor *mon, const QDict *qdict)
      }
  }
 
+static USBDevice *usb_device_from_bus_dot_addr(const char *devname)
+{
+    int bus_num, addr;
+    const char *p;
+    USBBus *bus;
+    USBPort *port;
+
+    if (!usb_enabled) {
+        return NULL;
+    }
+    p = strchr(devname, '.');
+    if (!p) {
+        return NULL;
+    }
+    bus_num = strtoul(devname, NULL, 0);
+    addr = strtoul(p + 1, NULL, 0);
+    bus = usb_bus_find(bus_num);
+    if (!bus) {
+        return NULL;
+    }
+    QTAILQ_FOREACH(port,&bus->used, next) {
+        if (port->dev->addr == addr) {
+            break;
+        }
+    }
+    if (!port) {
+        return NULL;
+    }
+    return port->dev;
+}
+
+void do_usb_detach(Monitor *mon, const QDict *qdict)
+{
+    const char *devname = qdict_get_str(qdict, "devname");
+    USBDevice *dev = usb_device_from_bus_dot_addr(devname);
+
+    if (dev == NULL || usb_device_detach(dev)<  0) {
+        error_report("could not detach USB device '%s'", devname);
+    }
+}
+
  /***********************************************************/
  /* PCMCIA/Cardbus */
 
author
#3 lore raw
Quoted 4 lines · ----- "Anthony Liguori

How is this different than usb_del? Is it that it detaches it but does
not delete the device?

yes. There is no usb_attach command because it was harder to write (can't use the bus.addr since a detached device doesn't have them) and I didn't need it right now, my device attaches itself based on a external event.

Quoted 109 lines

Regards,

Anthony Liguori

qemu-monitor.hx command @code{info usb} to see the devices you

Quoted 13 lines · Alon Levy
---
  qemu-monitor.hx |   17 +++++++++++++++++
  sysemu.h        |    1 +
  vl.c            |   41 +++++++++++++++++++++++++++++++++++++++++
  3 files changed, 59 insertions(+), 0 deletions(-)
qemu-monitor.hx +0 -0
index 49bcd8d..9c792a9 100644
@@ -711,6 +711,23 @@ command @code{info usb} to see the devices you

can remove.

vl.c void do_usb_del(Monitor *mon, const QDict

Quoted 40 lines
qemu-monitor.hx +17 -0
  ETEXI
 
      {
+        .name       = "usb_detach",
+        .args_type  = "devname:s",
+        .params     = "device",
+        .help       = "detach USB device 'bus.addr'",
+        .mhandler.cmd = do_usb_detach,
+    },
+
+STEXI
+@item usb_detach @var{devname}
+@findex usb_detach
+
+Detach the USB device @var{devname} from the QEMU virtual USB
+hub. @var{devname} has the syntax @code{bus.addr}. Use the monitor
+command @code{info usb} to see the devices you can detach.
+ETEXI
+
+    {
          .name       = "device_add",
          .args_type  = "device:O",
          .params     = "driver[,prop=value][,...]",
sysemu.h +1 -0
index a1f6466..ac68863 100644
@@ -183,6 +183,7 @@ extern struct soundhw soundhw[];
 
  void do_usb_add(Monitor *mon, const QDict *qdict);
  void do_usb_del(Monitor *mon, const QDict *qdict);
+void do_usb_detach(Monitor *mon, const QDict *qdict);
  void usb_info(Monitor *mon);
 
  void rtc_change_mon_event(struct tm *tm);
vl.c +0 -0
index d352d18..6cfa009 100644
@@ -891,6 +891,47 @@ void do_usb_del(Monitor *mon, const QDict

*qdict)

vl.c void do_usb_del(Monitor *mon, const QDict · line 894

Quoted 4 lines
vl.c +1 -0
      }
  }
 
+static USBDevice *usb_device_from_bus_dot_addr(const char

*devname)

Quoted 44 lines
vl.c +40 -0
+{
+    int bus_num, addr;
+    const char *p;
+    USBBus *bus;
+    USBPort *port;
+
+    if (!usb_enabled) {
+        return NULL;
+    }
+    p = strchr(devname, '.');
+    if (!p) {
+        return NULL;
+    }
+    bus_num = strtoul(devname, NULL, 0);
+    addr = strtoul(p + 1, NULL, 0);
+    bus = usb_bus_find(bus_num);
+    if (!bus) {
+        return NULL;
+    }
+    QTAILQ_FOREACH(port,&bus->used, next) {
+        if (port->dev->addr == addr) {
+            break;
+        }
+    }
+    if (!port) {
+        return NULL;
+    }
+    return port->dev;
+}
+
+void do_usb_detach(Monitor *mon, const QDict *qdict)
+{
+    const char *devname = qdict_get_str(qdict, "devname");
+    USBDevice *dev = usb_device_from_bus_dot_addr(devname);
+
+    if (dev == NULL || usb_device_detach(dev)<  0) {
+        error_report("could not detach USB device '%s'", devname);
+    }
+}
+
  /***********************************************************/
  /* PCMCIA/Cardbus */
 
#4 lore raw
Quoted 10 lines · Alon Levy
Quoted 3 lines · ----- "Anthony Liguori

How is this different than usb_del? Is it that it detaches it but does
not delete the device?

yes. There is no usb_attach command because it was harder to write (can't use the bus.addr since a detached device doesn't have them) and I didn't need it right now, my device attaches itself based on a external event.

Which points out a problem with this patch: It should better not use bus.addr. addr isn't fixed and even can be uninitialized. Yes, usb_del uses it (for historical reasons). But we better should not use it in new code. Better use the device id (like device_del). Which will work for usb_attach too.

Next question: What is the use case? attach/detach is used by devices internally. usb-host does attach/detach when devices get plugged-in and -out on the host. The ccid device does simliar things on vsclient connect/disconnect. So toggeling the attach state via monitor easily could have unwanted side effects ...

cheers,
Gerd

author
#5 lore raw
Quoted 25 lines · ----- "Gerd Hoffmann
Quoted 8 lines · Alon Levy
Quoted 3 lines · ----- "Anthony Liguori

How is this different than usb_del? Is it that it detaches it but does
not delete the device?

yes. There is no usb_attach command because it was harder to write

(can't

Quoted 1 line

use the bus.addr since a detached device doesn't have them) and I

didn't

Quoted 1 line

need it right now, my device attaches itself based on a external

event.

Which points out a problem with this patch: It should better not use

bus.addr. addr isn't fixed and even can be uninitialized. Yes, usb_del
uses it (for historical reasons). But we better should not use it in

new code. Better use the device id (like device_del). Which will work
for usb_attach too.

Quoted 7 lines

Next question: What is the use case? attach/detach is used by devices
internally. usb-host does attach/detach when devices get plugged-in and
-out on the host. The ccid device does simliar things on vsclient connect/disconnect. So toggeling the attach state via monitor easily

debugging. naturally when developing the ccid I had cases where I'd rather detach the device then bring down qemu. since there is no way currently to add/remove chardev's from monitor, removing/adding a device is not enough to reset a device state to the state right after start.

Quoted 4 lines

could have unwanted side effects ...

cheers,
Gerd