[Concept,15/33] test: usb: Reset usb_started at start of usb_stop test

Message ID 20260416023021.626949-16-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Running bootctl_logic_tkey before dm_test_usb_stop causes the latter
to fail: uclass_get_device(UCLASS_MASS_STORAGE, 0, ...) returns
-ENODEV and count_usb_devices() returns 2 instead of 7.

The bootctl test scans USB during setup, which sets the global
usb_started flag. When the test ends, dm_test_post_run() destroys
every uclass, removing the USB devices, but usb_started stays true.
The usb_stop test has UTF_SCAN_PDATA | UTF_SCAN_FDT rather than
UTF_DM, so no driver-model reset happens before it runs, and its
usb_init() call short-circuits on the stale flag without scanning
any buses.

Clear usb_started at the start of the test so usb_init() performs
a real scan regardless of what state an earlier test left behind.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/dm/usb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
  

Patch

diff --git a/test/dm/usb.c b/test/dm/usb.c
index 66777b5ef00..b3cf8ce8c36 100644
--- a/test/dm/usb.c
+++ b/test/dm/usb.c
@@ -152,6 +152,14 @@  static int dm_test_usb_stop(struct unit_test_state *uts)
 {
 	struct udevice *dev;
 
+	/*
+	 * A previous test (e.g. bootctl_logic_tkey) may have left the
+	 * global usb_started flag set while the USB uclass was then
+	 * destroyed by dm_test_post_run(). Reset it here so usb_init()
+	 * actually scans the buses, rather than short-circuiting.
+	 */
+	usb_started = false;
+
 	/* Scan and check that all devices are present */
 	state_set_skip_delays(true);
 	ut_assertok(usb_init());