[PATCH 08/11] tools/usb/p9_fwd: catch if claim_interface is not working

Michael Grzeschik posted 11 patches 2 weeks, 4 days ago
[PATCH 08/11] tools/usb/p9_fwd: catch if claim_interface is not working
Posted by Michael Grzeschik 2 weeks, 4 days ago
It is possible that some other tool was already claiming the interface.
Bail out gracefully if this happens.

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
 tools/usb/p9_fwd.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/usb/p9_fwd.py b/tools/usb/p9_fwd.py
index 12c76cbb046b7277bdd0ec39b663a041c9a1fc89..6a0104935ee995ec4159e27e1cfb7d93db422a33 100755
--- a/tools/usb/p9_fwd.py
+++ b/tools/usb/p9_fwd.py
@@ -79,7 +79,13 @@ class Forwarder:
             raise ValueError("Interface not found")
 
         logging.info(f"claiming interface:\n{usb9pfs}")
-        usb.util.claim_interface(dev, usb9pfs.bInterfaceNumber)
+        try:
+            usb.util.claim_interface(dev, usb9pfs.bInterfaceNumber)
+        except usb.core.USBError as e:
+            if e.errno == errno.EBUSY:
+                logging.debug("old connection was hanging", repr(e))
+                raise ValueError("disconnected")
+
         ep_out = usb.util.find_descriptor(
             usb9pfs,
             custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT,

-- 
2.47.3