From owner-dev-commits-src-all@freebsd.org Fri Jun 11 15:07:50 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 84B6D660714; Fri, 11 Jun 2021 15:07:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4G1kjf3PJhz3jRK; Fri, 11 Jun 2021 15:07:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5CCB72501B; Fri, 11 Jun 2021 15:07:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 15BF7ouU090321; Fri, 11 Jun 2021 15:07:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 15BF7oRL090320; Fri, 11 Jun 2021 15:07:50 GMT (envelope-from git) Date: Fri, 11 Jun 2021 15:07:50 GMT Message-Id: <202106111507.15BF7oRL090320@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hans Petter Selasky Subject: git: 6847ea50196f - main - Improve handling of USB device re-open in the LibUSB v1.x API. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hselasky X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6847ea50196f1a685be408a24f01cb8d407da19c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jun 2021 15:07:50 -0000 The branch main has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=6847ea50196f1a685be408a24f01cb8d407da19c commit 6847ea50196f1a685be408a24f01cb8d407da19c Author: Hans Petter Selasky AuthorDate: 2021-06-11 15:06:10 +0000 Commit: Hans Petter Selasky CommitDate: 2021-06-11 15:06:44 +0000 Improve handling of USB device re-open in the LibUSB v1.x API. Make sure the "device_is_gone" flag is cleared after every successful open, so that the "device_is_gone" flag doesn't persist forever. Found by: sergii.dmytruk@3mdeb.com PR: 256296 MFC after: 1 week Sponsored by: Mellanox Technologies // NVIDIA Networking --- lib/libusb/libusb10.c | 9 +++++++++ lib/libusb/libusb10_io.c | 12 ++++++++++-- lib/libusb/libusb20.c | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 1c45b87d8f0b..ffe0cf3f366a 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -529,6 +529,15 @@ libusb_open(libusb_device *dev, libusb_device_handle **devh) libusb_unref_device(dev); return (LIBUSB_ERROR_NO_MEM); } + + /* + * Clear the device gone flag, in case the device was opened + * after a re-attach, to allow new transaction: + */ + CTX_LOCK(ctx); + dev->device_is_gone = 0; + CTX_UNLOCK(ctx); + libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); diff --git a/lib/libusb/libusb10_io.c b/lib/libusb/libusb10_io.c index 53f5b040436d..0e32fc31c8e0 100644 --- a/lib/libusb/libusb10_io.c +++ b/lib/libusb/libusb10_io.c @@ -165,8 +165,16 @@ libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv) err = libusb20_dev_process(ppdev[i]); if (err) { - /* set device is gone */ - dev->device_is_gone = 1; + /* + * When the device is opened + * set the "device_is_gone" + * flag. This prevents the + * client from submitting new + * USB transfers to a detached + * device. + */ + if (ppdev[i]->is_opened) + dev->device_is_gone = 1; /* remove USB device from polling loop */ libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); diff --git a/lib/libusb/libusb20.c b/lib/libusb/libusb20.c index 6c2bf721bea9..4323552b83ad 100644 --- a/lib/libusb/libusb20.c +++ b/lib/libusb/libusb20.c @@ -608,6 +608,11 @@ libusb20_dev_close(struct libusb20_device *pdev) pdev->is_opened = 0; + /* + * Make sure libusb20_tr_get_pointer() fails: + */ + pdev->nTransfer = 0; + /* * The following variable is only used by the libusb v0.1 * compat layer: