From nobody Mon Feb 9 05:00:34 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1592390953; cv=none; d=zohomail.com; s=zohoarc; b=ggvRy+ZzTcdOudmqAE3Wo+fmUT1lj7StKWRHdmZDa+NR8gcFHArooGSuTMpGY4/i744BDEFR/HHdYXKag4mNscj6YynkMrHmI8yIuBdodB9s6uOp2vxJ7cUVErkaAHz+F3c5UnDBlBfkl/oP00sMCMyrXLWTudYvwkhAWqKLJHQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592390953; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=xhmwee+Y4JGFaQc53CHJpDOrdxOLP9hAvf81Ykk3JtY=; b=RVzfBqCruXmZX/tLIua305iWvygif9j/NvHTXTsCwY9yu5izskIO8MA/B/8fTbaO+u/PvIpTHND62D/xdI1rNmHYDywhNg7Q6dJJn34C0DeBDHR13mazoCW8RF+neo/3CAVqGrtGNuJv6neHVXfJlzpdre+uRPs8AGn2LYXHHpY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1592390953098475.8304089029732; Wed, 17 Jun 2020 03:49:13 -0700 (PDT) Received: from localhost ([::1]:37904 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVd1-000793-Rm for importer@patchew.org; Wed, 17 Jun 2020 06:49:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59622) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlVbu-0005ak-Jq for qemu-devel@nongnu.org; Wed, 17 Jun 2020 06:48:02 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:47342 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jlVbs-0002gJ-NV for qemu-devel@nongnu.org; Wed, 17 Jun 2020 06:48:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-49-OCVD7lTfPMSM0GF6AVysVA-1; Wed, 17 Jun 2020 06:47:55 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E3A8D80F5E9 for ; Wed, 17 Jun 2020 10:47:54 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-112-67.ams2.redhat.com [10.36.112.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF6D97CACF; Wed, 17 Jun 2020 10:47:51 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 875A71750C; Wed, 17 Jun 2020 12:47:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592390879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type:in-reply-to:in-reply-to: references:references; bh=xhmwee+Y4JGFaQc53CHJpDOrdxOLP9hAvf81Ykk3JtY=; b=GUM0BgyaGLzbR9On4r32xdN8bF/5YgCu1UKFsiGSorFdIlIhsRDONDtq4lJOrOXmCIKtZk g6YKI52hkpOteQItyecOIhUzgOH2JNgZN42GkBPmVvacOQZBhW3LiJUkdgPrKPzDL2dO7j HbAgJZxT69ILmhXcIGLOyqEIl817djA= X-MC-Unique: OCVD7lTfPMSM0GF6AVysVA-1 From: Gerd Hoffmann To: qemu-devel@nongnu.org Subject: [PULL 1/2] usb: add hostdevice property to usb-host Date: Wed, 17 Jun 2020 12:47:49 +0200 Message-Id: <20200617104750.29511-2-kraxel@redhat.com> In-Reply-To: <20200617104750.29511-1-kraxel@redhat.com> References: <20200617104750.29511-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kraxel@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/17 01:42:42 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The new property allows to specify usb host device name. Uses standard qemu_open(), so both file system path (/dev/bus/usb/$bus/$dev on linux) and file descriptor passing can be used. Requires libusb 1.0.23 or newer. The hostdevice property is only present in case qemu is compiled against a new enough library version, so the presence of the property can be used for feature detection. Signed-off-by: Gerd Hoffmann Message-Id: <20200605125952.13113-1-kraxel@redhat.com> --- hw/usb/host-libusb.c | 75 +++++++++++++++++++++++++++++++++++--------- hw/usb/trace-events | 1 + 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index e28441379d99..e88db544bc9e 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -80,6 +80,7 @@ struct USBHostDevice { =20 /* properties */ struct USBAutoFilter match; + char *hostdevice; int32_t bootindex; uint32_t iso_urb_count; uint32_t iso_urb_frames; @@ -97,6 +98,7 @@ struct USBHostDevice { int addr; char port[16]; =20 + int hostfd; libusb_device *dev; libusb_device_handle *dh; struct libusb_device_descriptor ddesc; @@ -880,26 +882,45 @@ static void usb_host_ep_update(USBHostDevice *s) libusb_free_config_descriptor(conf); } =20 -static int usb_host_open(USBHostDevice *s, libusb_device *dev) +static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd) { USBDevice *udev =3D USB_DEVICE(s); - int bus_num =3D libusb_get_bus_number(dev); - int addr =3D libusb_get_device_address(dev); + int bus_num =3D 0; + int addr =3D 0; int rc; Error *local_err =3D NULL; =20 if (s->bh_postld_pending) { return -1; } - - trace_usb_host_open_started(bus_num, addr); - if (s->dh !=3D NULL) { goto fail; } - rc =3D libusb_open(dev, &s->dh); - if (rc !=3D 0) { - goto fail; + + if (dev) { + bus_num =3D libusb_get_bus_number(dev); + addr =3D libusb_get_device_address(dev); + trace_usb_host_open_started(bus_num, addr); + + rc =3D libusb_open(dev, &s->dh); + if (rc !=3D 0) { + goto fail; + } + } else { +#if LIBUSB_API_VERSION >=3D 0x01000107 + trace_usb_host_open_hostfd(hostfd); + + rc =3D libusb_wrap_sys_device(ctx, hostfd, &s->dh); + if (rc !=3D 0) { + goto fail; + } + s->hostfd =3D hostfd; + dev =3D libusb_get_device(s->dh); + bus_num =3D libusb_get_bus_number(dev); + addr =3D libusb_get_device_address(dev); +#else + g_assert_not_reached(); +#endif } =20 s->dev =3D dev; @@ -988,6 +1009,11 @@ static int usb_host_close(USBHostDevice *s) s->dh =3D NULL; s->dev =3D NULL; =20 + if (s->hostfd !=3D -1) { + close(s->hostfd); + s->hostfd =3D -1; + } + usb_host_auto_check(NULL); return 0; } @@ -1025,9 +1051,6 @@ static libusb_device *usb_host_find_ref(int bus, int = addr) libusb_device *ret =3D NULL; int i, n; =20 - if (usb_host_init() !=3D 0) { - return NULL; - } n =3D libusb_get_device_list(ctx, &devs); for (i =3D 0; i < n; i++) { if (libusb_get_bus_number(devs[i]) =3D=3D bus && @@ -1046,6 +1069,10 @@ static void usb_host_realize(USBDevice *udev, Error = **errp) libusb_device *ldev; int rc; =20 + if (usb_host_init() !=3D 0) { + error_setg(errp, "failed to init libusb"); + return; + } if (s->match.vendor_id > 0xffff) { error_setg(errp, "vendorid out of range"); return; @@ -1064,7 +1091,24 @@ static void usb_host_realize(USBDevice *udev, Error = **errp) udev->auto_attach =3D 0; QTAILQ_INIT(&s->requests); QTAILQ_INIT(&s->isorings); + s->hostfd =3D -1; =20 +#if LIBUSB_API_VERSION >=3D 0x01000107 + if (s->hostdevice) { + int fd; + s->needs_autoscan =3D false; + fd =3D qemu_open(s->hostdevice, O_RDWR); + if (fd < 0) { + error_setg_errno(errp, errno, "failed to open %s", s->hostdevi= ce); + return; + } + rc =3D usb_host_open(s, NULL, fd); + if (rc < 0) { + error_setg(errp, "failed to open host usb device %s", s->hostd= evice); + return; + } + } else +#endif if (s->match.addr && s->match.bus_num && !s->match.vendor_id && !s->match.product_id && @@ -1077,7 +1121,7 @@ static void usb_host_realize(USBDevice *udev, Error *= *errp) s->match.bus_num, s->match.addr); return; } - rc =3D usb_host_open(s, ldev); + rc =3D usb_host_open(s, ldev, 0); libusb_unref_device(ldev); if (rc < 0) { error_setg(errp, "failed to open host usb device %d:%d", @@ -1605,6 +1649,9 @@ static Property usb_host_dev_properties[] =3D { DEFINE_PROP_STRING("hostport", USBHostDevice, match.port), DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0), DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0), +#if LIBUSB_API_VERSION >=3D 0x01000107 + DEFINE_PROP_STRING("hostdevice", USBHostDevice, hostdevice), +#endif DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4), DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32), DEFINE_PROP_BOOL("guest-reset", USBHostDevice, @@ -1723,7 +1770,7 @@ static void usb_host_auto_check(void *unused) if (s->dh !=3D NULL) { continue; } - if (usb_host_open(s, devs[i]) < 0) { + if (usb_host_open(s, devs[i], 0) < 0) { s->errcount++; continue; } diff --git a/hw/usb/trace-events b/hw/usb/trace-events index 5817ce4421f5..e9cdeeed1472 100644 --- a/hw/usb/trace-events +++ b/hw/usb/trace-events @@ -291,6 +291,7 @@ usb_mtp_file_monitor_event(int dev, const char *path, c= onst char *s) "dev %d, pa =20 # host-libusb.c usb_host_open_started(int bus, int addr) "dev %d:%d" +usb_host_open_hostfd(int hostfd) "hostfd %d" usb_host_open_success(int bus, int addr) "dev %d:%d" usb_host_open_failure(int bus, int addr) "dev %d:%d" usb_host_close(int bus, int addr) "dev %d:%d" --=20 2.18.4 From nobody Mon Feb 9 05:00:34 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1592390949; cv=none; d=zohomail.com; s=zohoarc; b=dAYEthaInx+oR7kgDy/hr9F24/RgkAIt0KnAm5lLbEjtZscCxE1mnHFO9llhFxcSMbF8kYPRmyaWOIbECQITkUR7nEY0EsGq13+tZApCNmNNBxg+jbnPasWeR2+Z/ixNWeG9RE5qN65rSKq0C8d5J58/+jWf8pLh1jwU/Dlm+dQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592390949; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=O9zf9n7GEiVsmXcOFimTPQREBfNNWSvJ/Vo90Qn26VE=; b=X/fKdRk70JNHjaDw2JO2JsXeetV43U5eOvFuiJXos8V7WvJUuzL+AAStSr8nXQ7dazaE4Oc8Y8p/Yqha5JAWBfcZvQVzWmYnOEpMHwneYV3DO5QDf2XOqsr2JsooTPZVHrZVtrBG/YCHt3C5k7Xl5e7xZRVOIqNEu5U1uhLDjZ0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1592390949003547.4984890880503; Wed, 17 Jun 2020 03:49:09 -0700 (PDT) Received: from localhost ([::1]:37470 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVcx-0006yF-Hr for importer@patchew.org; Wed, 17 Jun 2020 06:49:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlVbr-0005Vp-RQ for qemu-devel@nongnu.org; Wed, 17 Jun 2020 06:47:59 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55766 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jlVbq-0002fj-3f for qemu-devel@nongnu.org; Wed, 17 Jun 2020 06:47:59 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-356-iMjN69rXNEqbuQWFCR2zWg-1; Wed, 17 Jun 2020 06:47:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BC9DF10A86C8 for ; Wed, 17 Jun 2020 10:47:54 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-112-67.ams2.redhat.com [10.36.112.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB7A45C1C3; Wed, 17 Jun 2020 10:47:51 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 901E51753C; Wed, 17 Jun 2020 12:47:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592390877; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type:in-reply-to:in-reply-to: references:references; bh=O9zf9n7GEiVsmXcOFimTPQREBfNNWSvJ/Vo90Qn26VE=; b=TS2CVvFI9dzr5GCgQzCjPUBxuaQW+HvUbvP2UXoni9GAoDWoH8NuOH9zCuqRhSDbGXG1BS R1iBOFCK3IBF4p18QfZK7XUSdioD/AbmvXKaZyxs2YGSnPZJnBnQ5qNjM4yTSwPtM/+xr1 ofRVlSD1DaLOzbVxiLZS2CsPiOjCkHY= X-MC-Unique: iMjN69rXNEqbuQWFCR2zWg-1 From: Gerd Hoffmann To: qemu-devel@nongnu.org Subject: [PULL 2/2] usb-host: workaround libusb bug Date: Wed, 17 Jun 2020 12:47:50 +0200 Message-Id: <20200617104750.29511-3-kraxel@redhat.com> In-Reply-To: <20200617104750.29511-1-kraxel@redhat.com> References: <20200617104750.29511-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=kraxel@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/16 23:30:45 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" libusb seems to no allways call the completion callback for requests canceled (which it is supposed to do according to the docs). So add a limit to avoid qemu waiting forever. Tested-by: BALATON Zoltan Signed-off-by: Gerd Hoffmann Message-Id: <20200529072225.3195-1-kraxel@redhat.com> --- hw/usb/host-libusb.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index e88db544bc9e..ad7ed8fb0c67 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -972,6 +972,7 @@ fail: static void usb_host_abort_xfers(USBHostDevice *s) { USBHostRequest *r, *rtmp; + int limit =3D 100; =20 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { usb_host_req_abort(r); @@ -982,6 +983,19 @@ static void usb_host_abort_xfers(USBHostDevice *s) memset(&tv, 0, sizeof(tv)); tv.tv_usec =3D 2500; libusb_handle_events_timeout(ctx, &tv); + if (--limit =3D=3D 0) { + /* + * Don't wait forever for libusb calling the complete + * callback (which will unlink and free the request). + * + * Leaking memory here, to make sure libusb will not + * access memory which we have released already. + */ + QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { + QTAILQ_REMOVE(&s->requests, r, next); + } + return; + } } } =20 --=20 2.18.4