diff --git a/src/controller/emulated.rs b/src/controller/emulated.rs index 5efb98c..0e004a6 100644 --- a/src/controller/emulated.rs +++ b/src/controller/emulated.rs @@ -1,3 +1,8 @@ +use std::thread; +use std::fs::File; +use std::io::{Read, Write}; +use std::process::Command; + use crate::state::ControllerState; mod dgoc44u; mod tcpp20009; @@ -14,6 +19,7 @@ pub enum ControllerModel { pub fn set_model(state: &ControllerState) -> ControllerModel { if state.button_right { println!("Selected controller DGOC44-U."); + init_gadget(); return ControllerModel::DGOC44U; } else if state.button_up { @@ -44,3 +50,29 @@ pub fn set_state(state: &mut ControllerState, model: &ControllerModel) { _ => (), } } + +fn init_gadget() { + Command::new("modprobe").args(["g_ffs"]).output(); + Command::new("mkdir").args(["-p","/tmp/ffs-mascon"]).output(); + Command::new("mount").args(["-t","functionfs","mascon","/tmp/ffs-mascon"]).output(); + + let descriptors = [0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, + 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x00]; + let strings = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; + + thread::spawn(move || { + if let Ok(mut ep0) = File::open("/tmp/ffs-mascon/ep0") { + let mut buffer = [0; 100]; + loop { + ep0.read(&mut buffer).ok(); + } + } + }); + if let Ok(mut ep0) = File::create("/tmp/ffs-mascon/ep0") { + ep0.write(&descriptors).ok(); + println!("USB Gadget: Descriptors written to EP0"); + ep0.write(&strings).ok(); + println!("USB Gadget: Strings written to EP0"); + } +}