[PATCH 09/11] tools/usb/p9_fwd: catch write or read errors on disconnect

From: Michael Grzeschik

Date: Thu Mar 19 2026 - 05:46:18 EST


When unplugging the gadget or the gadget is not available
since the controller is reset, running transfers will return
with errno ENODEV. We catch this issues and report the problem
gracefully.

Signed-off-by: Michael Grzeschik <m.grzeschik@xxxxxxxxxxxxxx>
---
tools/usb/p9_fwd.py | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/tools/usb/p9_fwd.py b/tools/usb/p9_fwd.py
index 6a0104935ee995ec4159e27e1cfb7d93db422a33..c2c2afbf45df68376907ea2ae8c022614e4508f7 100755
--- a/tools/usb/p9_fwd.py
+++ b/tools/usb/p9_fwd.py
@@ -123,6 +123,9 @@ class Forwarder:
logging.debug("c2s: reading failed with %s, retrying", repr(e))
time.sleep(0.5)
continue
+ elif e.errno == errno.ENODEV:
+ logging.debug("c2s: reading failed with %s", repr(e))
+ raise ValueError("disconnected")
logging.error("c2s: reading failed with %s, aborting", repr(e))
raise
size = struct.unpack("<I", data[:4])[0]
@@ -145,9 +148,14 @@ class Forwarder:
logging.log(logging.TRACE, "s2c: writing")
self._log_hexdump(data)
while data:
- written = self.ep_out.write(data)
- assert written > 0
- data = data[written:]
+ try:
+ written = self.ep_out.write(data)
+ assert written > 0
+ data = data[written:]
+ except usb.core.USBError as e:
+ if e.errno == errno.EIO or e.errno == errno.ENODEV:
+ raise ValueError("disconnected")
+ raise
if size % self.ep_out.wMaxPacketSize == 0:
logging.log(logging.TRACE, "sending zero length packet")
self.ep_out.write(b"")

--
2.47.3