diff --git a/server/src/usb_gadget.rs b/server/src/usb_gadget.rs index d3a63d6..4030444 100644 --- a/server/src/usb_gadget.rs +++ b/server/src/usb_gadget.rs @@ -85,7 +85,7 @@ impl UsbGadget { /* 1 – detach gadget */ info!("🔌 detaching gadget from {ctrl}"); - Self::write_attr(self.udc_file, "")?; + Self::write_attr(self.udc_file, "none")?; Self::wait_state(&ctrl, "not attached", 3_000)?; /* 2 – reset driver */ @@ -97,13 +97,27 @@ impl UsbGadget { /* 4 – re‑attach + pull‑up */ info!("🔌 re‑attaching gadget to {ctrl}"); Self::write_attr(self.udc_file, &ctrl)?; - info!("Wrote udc file"); let sc = format!("/sys/class/udc/{ctrl}/soft_connect"); - Self::write_attr(&sc, "0")?; - info!("Wrote soft connect file 0"); - thread::sleep(Duration::from_millis(50)); - Self::write_attr(&sc, "1")?; - info!("Wrote soft connect file 1"); + if Path::new(&sc).exists() { + // try to set the pull-up; ignore if the kernel rejects it + match Self::write_attr(&sc, "1") { + Err(err) => { + // only swallow specific errno values + if let Some(io) = err.downcast_ref::() { + match io.raw_os_error() { + // EINVAL | EPERM | ENOENT + Some(libc::EINVAL) | Some(libc::EPERM) | Some(libc::ENOENT) => { + warn!("soft_connect unsupported ({io}); continuing"); + } + _ => return Err(err), // propagate all other errors + } + } else { + return Err(err); // non-IO errors: propagate + } + } + Ok(_) => { /* success */ } + } + } /* 5 – wait for host (but tolerate sleep) */ Self::wait_state(&ctrl, "configured", 6_000)