2025-06-25 21:02:29 -05:00
|
|
|
|
// server/src/usb_gadget.rs
|
2025-06-25 22:24:58 -05:00
|
|
|
|
use std::{fs::{self, OpenOptions}, io::Write, path::Path, thread, time::Duration};
|
2025-06-25 21:11:59 -05:00
|
|
|
|
use anyhow::{Context, Result};
|
2025-06-25 21:52:52 -05:00
|
|
|
|
use tracing::{info, warn, trace};
|
2025-06-24 23:48:06 -05:00
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
|
pub struct UsbGadget {
|
|
|
|
|
|
udc_file: &'static str,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-25 07:46:50 -05:00
|
|
|
|
impl UsbGadget {
|
|
|
|
|
|
pub fn new(name: &'static str) -> Self {
|
2025-06-25 21:02:29 -05:00
|
|
|
|
Self {
|
|
|
|
|
|
udc_file: Box::leak(
|
|
|
|
|
|
format!("/sys/kernel/config/usb_gadget/{name}/UDC").into_boxed_str(),
|
|
|
|
|
|
),
|
2025-06-24 23:48:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/*–––– helpers ––––*/
|
|
|
|
|
|
|
|
|
|
|
|
/// Find the first controller in /sys/class/udc (e.g. `1000480000.usb`)
|
|
|
|
|
|
fn find_controller() -> Result<String> {
|
|
|
|
|
|
Ok(fs::read_dir("/sys/class/udc")?
|
2025-06-25 21:52:52 -05:00
|
|
|
|
.next()
|
|
|
|
|
|
.transpose()?
|
|
|
|
|
|
.context("no UDC present")?
|
|
|
|
|
|
.file_name()
|
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
|
.into_owned())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/// Busy‑loop (≤ `limit_ms`) until `state` matches `wanted`
|
|
|
|
|
|
fn wait_state(ctrl: &str, wanted: &str, limit_ms: u64) -> Result<()> {
|
|
|
|
|
|
let path = format!("/sys/class/udc/{ctrl}/state");
|
|
|
|
|
|
for _ in 0..=limit_ms / 50 {
|
|
|
|
|
|
let s = fs::read_to_string(&path).unwrap_or_default();
|
|
|
|
|
|
trace!("⏳ state={s:?}, want={wanted}");
|
|
|
|
|
|
if s.trim() == wanted {
|
2025-06-25 21:11:59 -05:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
2025-06-25 21:52:52 -05:00
|
|
|
|
thread::sleep(Duration::from_millis(50));
|
2025-06-25 21:11:59 -05:00
|
|
|
|
}
|
2025-06-25 22:24:58 -05:00
|
|
|
|
Err(anyhow::anyhow!("UDC never reached '{wanted}' (last = {:?})",
|
|
|
|
|
|
fs::read_to_string(&path).unwrap_or_default()))
|
2025-06-25 21:11:59 -05:00
|
|
|
|
}
|
2025-06-24 23:48:06 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/// Write `value` (plus “\n”) into a sysfs attribute
|
|
|
|
|
|
fn write_attr<P: AsRef<Path>>(p: P, value: &str) -> Result<()> {
|
|
|
|
|
|
OpenOptions::new()
|
|
|
|
|
|
.write(true)
|
|
|
|
|
|
.open(p)?
|
|
|
|
|
|
.write_all(format!("{value}\n").as_bytes())?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
2025-06-25 21:52:52 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/*–––– public API ––––*/
|
2025-06-25 21:02:29 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/// Hard‑reset the gadget → identical to a physical cable re‑plug
|
|
|
|
|
|
pub fn cycle(&self) -> Result<()> {
|
|
|
|
|
|
/* 0 – ensure we *know* the controller even after a previous crash */
|
|
|
|
|
|
let ctrl = match Self::find_controller() {
|
|
|
|
|
|
Ok(c) => c,
|
|
|
|
|
|
Err(_) => {
|
|
|
|
|
|
// try to recover by reading the last name from the gadget’s UDC file
|
|
|
|
|
|
let last = fs::read_to_string(self.udc_file)?.trim().to_owned();
|
|
|
|
|
|
if last.is_empty() {
|
|
|
|
|
|
return Err(anyhow::anyhow!("no UDC present and UDC file is empty"));
|
|
|
|
|
|
}
|
|
|
|
|
|
warn!("⚠️ UDC missing, attempting to re‑bind {last}");
|
|
|
|
|
|
Self::rebind_driver(&last)?;
|
|
|
|
|
|
last
|
|
|
|
|
|
}
|
2025-06-25 21:52:52 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/* 1 – detach gadget */
|
|
|
|
|
|
info!("🔌 detaching gadget from {ctrl}");
|
|
|
|
|
|
Self::write_attr(self.udc_file, "")?;
|
|
|
|
|
|
Self::wait_state(&ctrl, "not attached", 3_000)?;
|
2025-06-25 21:52:52 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/* 2 – unbind / bind platform driver (dwc2/dwc3) */
|
|
|
|
|
|
Self::rebind_driver(&ctrl)?;
|
2025-06-25 21:52:52 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
/* 3 – re‑attach gadget */
|
2025-06-25 21:52:52 -05:00
|
|
|
|
info!("🔌 re‑attaching gadget to {ctrl}");
|
2025-06-25 22:24:58 -05:00
|
|
|
|
Self::write_attr(self.udc_file, &ctrl)?;
|
|
|
|
|
|
Self::wait_state(&ctrl, "configured", 6_000)?;
|
2025-06-25 21:02:29 -05:00
|
|
|
|
|
2025-06-25 22:24:58 -05:00
|
|
|
|
info!("✅ USB‑gadget cycle complete");
|
2025-06-25 21:11:59 -05:00
|
|
|
|
Ok(())
|
2025-06-24 23:48:06 -05:00
|
|
|
|
}
|
2025-06-25 22:24:58 -05:00
|
|
|
|
|
|
|
|
|
|
/// helper: unbind + 300 ms reset + bind
|
|
|
|
|
|
fn rebind_driver(ctrl: &str) -> Result<()> {
|
|
|
|
|
|
let cand = ["dwc2", "dwc3"]; // cover RasPi, RK, AM62, …
|
|
|
|
|
|
for drv in cand {
|
|
|
|
|
|
let root = format!("/sys/bus/platform/drivers/{drv}");
|
|
|
|
|
|
if !Path::new(&root).exists() {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
info!("🔧 unbinding UDC driver ({drv})");
|
|
|
|
|
|
Self::write_attr(format!("{root}/unbind"), ctrl)?;
|
|
|
|
|
|
thread::sleep(Duration::from_millis(300));
|
|
|
|
|
|
|
|
|
|
|
|
info!("🔧 binding UDC driver ({drv})");
|
|
|
|
|
|
Self::write_attr(format!("{root}/bind"), ctrl)?;
|
|
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(anyhow::anyhow!("no dwc2/dwc3 driver nodes found"))
|
|
|
|
|
|
}
|
2025-06-24 23:48:06 -05:00
|
|
|
|
}
|