tinyusb icon indicating copy to clipboard operation
tinyusb copied to clipboard

EHCI race condition on device disconnection

Open Ryzee119 opened this issue 3 years ago • 0 comments

Operating System

Windows 11

Board

Teensy4.1 (EHCI)

Firmware

When unplugging a device on my Teensy4 board running the host EHCI backend I reliably hit this assert, particularing when using a USB Hub:

https://github.com/hathach/tinyusb/blob/585f37a7977c3ff7e00b1e2235eb0712b12c90d6/src/host/usbh.c#L496

I believe this is due to a race condition during device removal. Consider the following sequence:

  1. Device is unplugged - TinyUSB registers a detach event in IRQ. 👍
  2. Prior to tusb processing the removal (race), In flight transfers will fail with STALL condition - TinyUSB registers a XFER_COMPLETE with STALL condition. 👍
  3. Eventually TinyUSB task loop will process the detach event and remove the associated driver. 👍
  4. TinyUSB will then process the XFER_COMPLETE against a now cleared device and will hit the above assert. 👎

Im not sure if this is present in other backends.

I think the fix would be to

  1. Not assert on this error assuming this only occurs if the device is disconnected
  2. Prior to creating the detach event, cancel infight transfers. This is probably the better solution.

Happy to raise a PR for this when I dig into it a bit more

What happened ?

How to reproduce ?

  1. Run an example with EHCI backend
  2. Connect device that has a running transfer - such as USB HUB or HID device.
  3. Remove device
  4. Can hit assert https://github.com/hathach/tinyusb/blob/585f37a7977c3ff7e00b1e2235eb0712b12c90d6/src/host/usbh.c#L496 if the highlighted race conditions are met.

Debug Log as txt file

No response

Screenshots

No response

Ryzee119 avatar Aug 16 '22 00:08 Ryzee119