monitor: add usb_detach
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
@@ -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
@@ -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]>
Quoted 1 line · On Tue, 5 Oct 2010 16:40:29 +0200 Alon Levy <[email protected]> wrote:
Signed-off-by Alon Levy <[email protected]>
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@@ -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@@ -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@@ -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 */
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
Signed-off-by Alon Levy<[email protected]>--- qemu-monitor.hx | 17 +++++++++++++++++ sysemu.h | 1 + vl.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 0 deletions(-)qemu-monitor.hx +17
-0@@ -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@@ -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@@ -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 */
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
Signed-off-by Alon Levy<[email protected]>--- qemu-monitor.hx | 17 +++++++++++++++++ sysemu.h | 1 + vl.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 0 deletions(-)qemu-monitor.hx +0
-0@@ -711,6 +711,23 @@ command @code{info usb} to see the devices youcan remove.
vl.c void do_usb_del(Monitor *mon, const QDict
Quoted 40 lines
qemu-monitor.hx +17
-0ETEXI { + .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@@ -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@@ -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 */
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_attachcommand 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
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_attachcommand 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 innew code. Better use the device id (like
device_del). Which will work
forusb_attachtoo.
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