From patchwork Wed Jun 16 08:48:23 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 55865 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DC2331007D2 for ; Wed, 16 Jun 2010 18:51:01 +1000 (EST) Received: from localhost ([127.0.0.1]:60251 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OOoKo-0004Cv-7h for incoming@patchwork.ozlabs.org; Wed, 16 Jun 2010 04:50:58 -0400 Received: from [140.186.70.92] (port=43945 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OOoIS-0002cX-Kd for qemu-devel@nongnu.org; Wed, 16 Jun 2010 04:48:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OOoIR-0007sF-EC for qemu-devel@nongnu.org; Wed, 16 Jun 2010 04:48:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38921) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OOoIR-0007s7-4D for qemu-devel@nongnu.org; Wed, 16 Jun 2010 04:48:31 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o5G8mT7s008510 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 16 Jun 2010 04:48:29 -0400 Received: from rincewind.home.kraxel.org (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o5G8mPjY020128; Wed, 16 Jun 2010 04:48:26 -0400 Message-ID: <4C188FD7.2050306@redhat.com> Date: Wed, 16 Jun 2010 10:48:23 +0200 From: Gerd Hoffmann User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100505 Fedora/3.0.4-2.el6 Thunderbird/3.0.4 MIME-Version: 1.0 To: Shahar Havivi Subject: Re: [Qemu-devel] [PATCH 2/2] Return usb device to host on exit References: <3e33b4dbeaba1f5788ae17ab16b3788f020a1af4.1276624758.git.shaharh@redhat.com> In-Reply-To: <3e33b4dbeaba1f5788ae17ab16b3788f020a1af4.1276624758.git.shaharh@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: qemu-devel@nongnu.org X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org > +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 From 731761de07b73555faf96dc466efd7db2480a694 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann 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 --- 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 #include @@ -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; }