* [Qemu-devel] [PATCH 0/2 v4] Release usb devices on shutdown and usb_del @ 2010-06-15 18:05 Shahar Havivi 2010-06-15 18:05 ` [Qemu-devel] [PATCH 1/2] Return usb device to host on usb_del command Shahar Havivi 2010-06-15 18:06 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 0 siblings, 2 replies; 8+ messages in thread From: Shahar Havivi @ 2010-06-15 18:05 UTC (permalink / raw) To: qemu-devel v4: use exit notifier instead of atexit() Shahar Havivi (2): Return usb device to host on usb_del command Return usb device to host on exit usb-linux.c | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 1/2] Return usb device to host on usb_del command 2010-06-15 18:05 [Qemu-devel] [PATCH 0/2 v4] Release usb devices on shutdown and usb_del Shahar Havivi @ 2010-06-15 18:05 ` Shahar Havivi 2010-06-15 18:06 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 1 sibling, 0 replies; 8+ messages in thread From: Shahar Havivi @ 2010-06-15 18:05 UTC (permalink / raw) To: qemu-devel Signed-off-by: Shahar Havivi <shaharh@redhat.com> --- usb-linux.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 88273ff..22a85e3 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -991,6 +991,7 @@ static int usb_host_close(USBHostDevice *dev) async_complete(dev); dev->closing = 0; usb_device_detach(&dev->dev); + ioctl(dev->fd, USBDEVFS_RESET); close(dev->fd); dev->fd = -1; return 0; -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-15 18:05 [Qemu-devel] [PATCH 0/2 v4] Release usb devices on shutdown and usb_del Shahar Havivi 2010-06-15 18:05 ` [Qemu-devel] [PATCH 1/2] Return usb device to host on usb_del command Shahar Havivi @ 2010-06-15 18:06 ` Shahar Havivi 2010-06-16 8:48 ` Gerd Hoffmann 1 sibling, 1 reply; 8+ messages in thread From: Shahar Havivi @ 2010-06-15 18:06 UTC (permalink / raw) To: qemu-devel Signed-off-by: Shahar Havivi <shaharh@redhat.com> --- usb-linux.c | 19 +++++++++++++++++++ 1 files changed, 19 insertions(+), 0 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 22a85e3..4b5aeb6 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -33,6 +33,7 @@ #include "qemu-common.h" #include "qemu-timer.h" #include "monitor.h" +#include "sysemu.h" #include <dirent.h> #include <sys/ioctl.h> @@ -89,6 +90,8 @@ static char *usb_host_device_path; #define USB_FS_SYS 3 static int usb_fs_type; +static int usb_notify_set; +static Notifier usb_host_notifier; /* endpoint association data */ struct endp_data { @@ -286,6 +289,17 @@ static void async_cancel(USBPacket *unused, void *opaque) } } +static void usb_host_cleanup(struct Notifier* n) +{ + struct USBHostDevice *s; + + QTAILQ_FOREACH(s, &hostdevs, next) { + if (s->fd != -1) { + ioctl(s->fd, USBDEVFS_RESET); + } + } +} + static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) { int dev_descr_len, config_descr_len; @@ -1066,6 +1080,11 @@ USBDevice *usb_host_device_open(const char *devname) qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id); qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id); qdev_init_nofail(&dev->qdev); + if (!usb_notify_set) { + usb_notify_set = 1; + usb_host_notifier.notify = usb_host_cleanup; + qemu_add_exit_notifier(&usb_host_notifier); + } return dev; fail: -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-15 18:06 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi @ 2010-06-16 8:48 ` Gerd Hoffmann 2010-06-16 10:18 ` Shahar Havivi 0 siblings, 1 reply; 8+ messages in thread From: Gerd Hoffmann @ 2010-06-16 8:48 UTC (permalink / raw) To: Shahar Havivi; +Cc: qemu-devel [-- Attachment #1: Type: text/plain, Size: 663 bytes --] > +static void usb_host_cleanup(struct Notifier* n) > +{ > + struct USBHostDevice *s; > + > + QTAILQ_FOREACH(s,&hostdevs, next) { > + if (s->fd != -1) { > + ioctl(s->fd, USBDEVFS_RESET); > + } > + } > +} Well. The point of exit notifiers is that you don't need global variables for your cleanup work because the notifier function gets passed in a handle to your state data. In that specific case the global hostdevs is needed anyway for other reasons. Nevertheless I don't want usb-linux.c set a bad example, but provide a good reference implementation for others to look at. Patch attached (untested). cheers, Gerd [-- Attachment #2: 0001-usb-host-make-sure-we-release-the-device.patch --] [-- Type: text/plain, Size: 2049 bytes --] >From 731761de07b73555faf96dc466efd7db2480a694 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann <kraxel@redhat.com> Date: Wed, 16 Jun 2010 10:29:59 +0200 Subject: [PATCH] usb-host: make sure we release the device. Call USBDEVFS_RESET ioctl in usb_host_close. Use exit notifiers to make sure we do it on exit too. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> --- usb-linux.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 88273ff..a089fb6 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -33,6 +33,7 @@ #include "qemu-common.h" #include "qemu-timer.h" #include "monitor.h" +#include "sysemu.h" #include <dirent.h> #include <sys/ioctl.h> @@ -132,6 +133,7 @@ typedef struct USBHostDevice { int configuration; int ninterfaces; int closing; + Notifier exit; struct ctrl_struct ctrl; struct endp_data endp_table[MAX_ENDPOINTS]; @@ -404,6 +406,7 @@ static void usb_host_handle_destroy(USBDevice *dev) usb_host_close(s); QTAILQ_REMOVE(&hostdevs, s, next); + qemu_remove_exit_notifier(&s->exit); } static int usb_linux_update_endp_table(USBHostDevice *s); @@ -991,11 +994,21 @@ static int usb_host_close(USBHostDevice *dev) async_complete(dev); dev->closing = 0; usb_device_detach(&dev->dev); + ioctl(s->fd, USBDEVFS_RESET); close(dev->fd); dev->fd = -1; return 0; } +static void usb_host_exit_notifier(struct Notifier* n) +{ + USBHostDevice *s = container_of(n, USBHostDevice, exit); + + if (s->fd != -1) { + ioctl(s->fd, USBDEVFS_RESET); + } +} + static int usb_host_initfn(USBDevice *dev) { USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); @@ -1003,6 +1016,8 @@ static int usb_host_initfn(USBDevice *dev) dev->auto_attach = 0; s->fd = -1; QTAILQ_INSERT_TAIL(&hostdevs, s, next); + s->exit.notify = usb_host_exit_notifier; + qemu_add_exit_notifier(&s->exit); usb_host_auto_check(NULL); return 0; } -- 1.6.5.2 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-16 8:48 ` Gerd Hoffmann @ 2010-06-16 10:18 ` Shahar Havivi 0 siblings, 0 replies; 8+ messages in thread From: Shahar Havivi @ 2010-06-16 10:18 UTC (permalink / raw) To: Gerd Hoffmann; +Cc: qemu-devel On Wed, Jun 16, 2010 at 10:48:23AM +0200, Gerd Hoffmann wrote: > Date: Wed, 16 Jun 2010 10:48:23 +0200 > From: Gerd Hoffmann <kraxel@redhat.com> > To: Shahar Havivi <shaharh@redhat.com> > Cc: qemu-devel@nongnu.org > Subject: Re: [Qemu-devel] [PATCH 2/2] Return usb device to host on exit > > >+static void usb_host_cleanup(struct Notifier* n) > >+{ > >+ struct USBHostDevice *s; > >+ > >+ QTAILQ_FOREACH(s,&hostdevs, next) { > >+ if (s->fd != -1) { > >+ ioctl(s->fd, USBDEVFS_RESET); > >+ } > >+ } > >+} > > Well. The point of exit notifiers is that you don't need global > variables for your cleanup work because the notifier function gets > passed in a handle to your state data. > > In that specific case the global hostdevs is needed anyway for other > reasons. Nevertheless I don't want usb-linux.c set a bad example, > but provide a good reference implementation for others to look at. > > Patch attached (untested). > > cheers, > Gerd Thanks for the info Gerd, I will test it. Shahar. > From 731761de07b73555faf96dc466efd7db2480a694 Mon Sep 17 00:00:00 2001 > From: Gerd Hoffmann <kraxel@redhat.com> > Date: Wed, 16 Jun 2010 10:29:59 +0200 > Subject: [PATCH] usb-host: make sure we release the device. > > Call USBDEVFS_RESET ioctl in usb_host_close. > Use exit notifiers to make sure we do it on exit too. > > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> > --- > usb-linux.c | 15 +++++++++++++++ > 1 files changed, 15 insertions(+), 0 deletions(-) > > diff --git a/usb-linux.c b/usb-linux.c > index 88273ff..a089fb6 100644 > --- a/usb-linux.c > +++ b/usb-linux.c > @@ -33,6 +33,7 @@ > #include "qemu-common.h" > #include "qemu-timer.h" > #include "monitor.h" > +#include "sysemu.h" > > #include <dirent.h> > #include <sys/ioctl.h> > @@ -132,6 +133,7 @@ typedef struct USBHostDevice { > int configuration; > int ninterfaces; > int closing; > + Notifier exit; > > struct ctrl_struct ctrl; > struct endp_data endp_table[MAX_ENDPOINTS]; > @@ -404,6 +406,7 @@ static void usb_host_handle_destroy(USBDevice *dev) > > usb_host_close(s); > QTAILQ_REMOVE(&hostdevs, s, next); > + qemu_remove_exit_notifier(&s->exit); > } > > static int usb_linux_update_endp_table(USBHostDevice *s); > @@ -991,11 +994,21 @@ static int usb_host_close(USBHostDevice *dev) > async_complete(dev); > dev->closing = 0; > usb_device_detach(&dev->dev); > + ioctl(s->fd, USBDEVFS_RESET); > close(dev->fd); > dev->fd = -1; > return 0; > } > > +static void usb_host_exit_notifier(struct Notifier* n) > +{ > + USBHostDevice *s = container_of(n, USBHostDevice, exit); > + > + if (s->fd != -1) { > + ioctl(s->fd, USBDEVFS_RESET); > + } > +} > + > static int usb_host_initfn(USBDevice *dev) > { > USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); > @@ -1003,6 +1016,8 @@ static int usb_host_initfn(USBDevice *dev) > dev->auto_attach = 0; > s->fd = -1; > QTAILQ_INSERT_TAIL(&hostdevs, s, next); > + s->exit.notify = usb_host_exit_notifier; > + qemu_add_exit_notifier(&s->exit); > usb_host_auto_check(NULL); > return 0; > } > -- > 1.6.5.2 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 0/2 v5] Release usb devices on shutdown and usb_del @ 2010-06-16 12:15 Shahar Havivi 2010-06-16 12:16 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 0 siblings, 1 reply; 8+ messages in thread From: Shahar Havivi @ 2010-06-16 12:15 UTC (permalink / raw) To: Gerd Hoffmann, qemu-devel v5: Fix to Gerd Hoffmann comments on v4. Shahar Havivi (2): Return usb device to host on usb_del command Return usb device to host on exit usb-linux.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-16 12:15 [Qemu-devel] [PATCH 0/2 v5] Release usb devices on shutdown and usb_del Shahar Havivi @ 2010-06-16 12:16 ` Shahar Havivi 0 siblings, 0 replies; 8+ messages in thread From: Shahar Havivi @ 2010-06-16 12:16 UTC (permalink / raw) To: Gerd Hoffmann, qemu-devel Signed-off-by: Shahar Havivi <shaharh@redhat.com> --- usb-linux.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 22a85e3..c3c38ec 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -33,6 +33,7 @@ #include "qemu-common.h" #include "qemu-timer.h" #include "monitor.h" +#include "sysemu.h" #include <dirent.h> #include <sys/ioctl.h> @@ -132,6 +133,7 @@ typedef struct USBHostDevice { int configuration; int ninterfaces; int closing; + Notifier exit; struct ctrl_struct ctrl; struct endp_data endp_table[MAX_ENDPOINTS]; @@ -404,6 +406,7 @@ static void usb_host_handle_destroy(USBDevice *dev) usb_host_close(s); QTAILQ_REMOVE(&hostdevs, s, next); + qemu_remove_exit_notifier(&s->exit); } static int usb_linux_update_endp_table(USBHostDevice *s); @@ -997,6 +1000,15 @@ static int usb_host_close(USBHostDevice *dev) return 0; } +static void usb_host_exit_notifier(struct Notifier* n) +{ + USBHostDevice *s = container_of(n, USBHostDevice, exit); + + if (s->fd != -1) { + ioctl(s->fd, USBDEVFS_RESET); + } +} + static int usb_host_initfn(USBDevice *dev) { USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); @@ -1004,6 +1016,8 @@ static int usb_host_initfn(USBDevice *dev) dev->auto_attach = 0; s->fd = -1; QTAILQ_INSERT_TAIL(&hostdevs, s, next); + s->exit.notify = usb_host_exit_notifier; + qemu_add_exit_notifier(&s->exit); usb_host_auto_check(NULL); return 0; } -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 0/2 v3] Release usb devices on shutdown and usb_del command @ 2010-06-12 9:58 Shahar Havivi 2010-06-12 9:59 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 0 siblings, 1 reply; 8+ messages in thread From: Shahar Havivi @ 2010-06-12 9:58 UTC (permalink / raw) To: qemu-devel v3: separate usb hot-unplug and host terminate handling remove empty methods from bsd and stub added usb-linux atexit method to reset usb devices on termination Shahar Havivi (2): Return usb device to host on usb_del command Return usb device to host on exit usb-linux.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-) ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-12 9:58 [Qemu-devel] [PATCH 0/2 v3] Release usb devices on shutdown and usb_del command Shahar Havivi @ 2010-06-12 9:59 ` Shahar Havivi 2010-06-14 9:50 ` Gerd Hoffmann 0 siblings, 1 reply; 8+ messages in thread From: Shahar Havivi @ 2010-06-12 9:59 UTC (permalink / raw) To: qemu-devel Signed-off-by: Shahar Havivi <shaharh@redhat.com> --- usb-linux.c | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 22a85e3..2a595f0 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -286,6 +286,17 @@ static void async_cancel(USBPacket *unused, void *opaque) } } +static void usb_host_cleanup(void) +{ + struct USBHostDevice *s; + + QTAILQ_FOREACH(s, &hostdevs, next) { + if (s->fd != -1) { + ioctl(s->fd, USBDEVFS_RESET); + } + } +} + static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) { int dev_descr_len, config_descr_len; @@ -1066,6 +1077,7 @@ USBDevice *usb_host_device_open(const char *devname) qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id); qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id); qdev_init_nofail(&dev->qdev); + atexit(usb_host_cleanup); return dev; fail: -- 1.7.0.4 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 2/2] Return usb device to host on exit 2010-06-12 9:59 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi @ 2010-06-14 9:50 ` Gerd Hoffmann 0 siblings, 0 replies; 8+ messages in thread From: Gerd Hoffmann @ 2010-06-14 9:50 UTC (permalink / raw) To: Shahar Havivi; +Cc: qemu-devel > @@ -1066,6 +1077,7 @@ USBDevice *usb_host_device_open(const char *devname) > qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id); > qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id); > qdev_init_nofail(&dev->qdev); > + atexit(usb_host_cleanup); > return dev; You'll register atexit multiple times here (once per device). I still think this should use exit notifiers, see http://patchwork.ozlabs.org/patch/54571/ (doesn't apply cleanly and more, will post a rebased version later today). cheers, Gerd ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2010-06-16 12:15 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2010-06-15 18:05 [Qemu-devel] [PATCH 0/2 v4] Release usb devices on shutdown and usb_del Shahar Havivi 2010-06-15 18:05 ` [Qemu-devel] [PATCH 1/2] Return usb device to host on usb_del command Shahar Havivi 2010-06-15 18:06 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 2010-06-16 8:48 ` Gerd Hoffmann 2010-06-16 10:18 ` Shahar Havivi -- strict thread matches above, loose matches on Subject: below -- 2010-06-16 12:15 [Qemu-devel] [PATCH 0/2 v5] Release usb devices on shutdown and usb_del Shahar Havivi 2010-06-16 12:16 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 2010-06-12 9:58 [Qemu-devel] [PATCH 0/2 v3] Release usb devices on shutdown and usb_del command Shahar Havivi 2010-06-12 9:59 ` [Qemu-devel] [PATCH 2/2] Return usb device to host on exit Shahar Havivi 2010-06-14 9:50 ` Gerd Hoffmann
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).