server udc fix

This commit is contained in:
Brad Stein 2025-06-25 23:27:51 -05:00
parent f4a9b4eb6f
commit 140eef5513
2 changed files with 25 additions and 2 deletions

View File

@ -51,7 +51,7 @@ fn init_tracing() -> anyhow::Result<WorkerGuard> {
}
async fn open_with_retry(path: &str) -> anyhow::Result<tokio::fs::File> {
const MAX_ATTEMPTS: usize = 40; // ≈ 2s (@50ms)
const MAX_ATTEMPTS: usize = 200; // ≈ 10s (@50ms)
for attempt in 1..=MAX_ATTEMPTS {
match OpenOptions::new()
.write(true)

View File

@ -84,7 +84,30 @@ impl UsbGadget {
/* 3 reattach gadget */
info!("🔌 reattaching gadget to {ctrl}");
Self::write_attr(self.udc_file, &ctrl)?;
Self::wait_state(&ctrl, "configured", 6_000)?;
/* 4 toggle gadget */
let sc = format!("/sys/class/udc/{ctrl}/soft_connect");
// toggle 0 → 1 to force the controller to assert pullups
Self::write_attr(&sc, "0")?; // guarantee clean edge
thread::sleep(Duration::from_millis(50));
Self::write_attr(&sc, "1")?;
/* 4 wait for gadget */
Self::wait_state(&ctrl, "configured", 6_000)
.or_else(|e| {
// If the host is physically absent (sleep / KVM paused)
// we allow 'not attached' and continue we can still
// accept keyboard/mouse data and the host will enumerate
// later without another reset.
let last = fs::read_to_string(format!("/sys/class/udc/{ctrl}/state"))
.unwrap_or_default();
if last.trim() == "not attached" {
warn!("⚠️ host did not enumerate within 6s continuing (state = {last:?})");
Ok(())
} else {
Err(e)
}
})?;
info!("✅ USBgadget cycle complete");
Ok(())