Implement gadget creation per controller

This commit is contained in:
Marc Riera 2023-03-17 00:35:16 +01:00
parent 6c8783bd93
commit 983e807fc7
7 changed files with 96 additions and 70 deletions

View file

@ -3,36 +3,46 @@ use std::fs::File;
use std::io::{Read, Write};
use std::process::Command;
use crate::state::ControllerState;
use crate::controller::physical::ControllerState;
mod dgoc44u;
mod tcpp20009;
mod vok00106;
#[derive(PartialEq)]
pub enum ControllerModel {
NONE,
DGOC44U,
TCPP20009,
VOK00106,
}
pub fn set_model(state: &ControllerState) -> ControllerModel {
pub struct DeviceDescriptor {
b_device_class: u8,
b_device_sub_class: u8,
id_vendor: u16,
id_product: u16,
i_manufacturer: &'static str,
i_product: &'static str,
i_serial_number: &'static str,
}
pub fn set_model(state: &ControllerState) -> Option<ControllerModel> {
if state.button_right {
println!("Selected controller DGOC44-U.");
init_gadget();
return ControllerModel::DGOC44U;
init_gadget(&dgoc44u::DEVICE_DESCRIPTOR, &dgoc44u::DESCRIPTORS, &dgoc44u::STRINGS);
return Some(ControllerModel::DGOC44U);
}
else if state.button_up {
println!("Selected controller TCPP-20009.");
return ControllerModel::TCPP20009;
init_gadget(&tcpp20009::DEVICE_DESCRIPTOR, &tcpp20009::DESCRIPTORS, &tcpp20009::STRINGS);
return Some(ControllerModel::TCPP20009);
}
else if state.button_a {
println!("Selected controller VOK-00106.");
return ControllerModel::VOK00106;
return Some(ControllerModel::VOK00106);
}
else {
println!("No controller selected.");
return ControllerModel::NONE;
return None;
}
}
@ -47,19 +57,21 @@ pub fn set_state(state: &mut ControllerState, model: &ControllerModel) {
ControllerModel::VOK00106 => {
vok00106::update_gadget(state);
}
_ => (),
}
}
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];
fn init_gadget(device: &DeviceDescriptor, descriptors: &[u8], strings: &[u8]) {
Command::new("modprobe").arg("g_ffs")
.arg(String::from("bDeviceClass=")+&device.b_device_class.to_string())
.arg(String::from("bDeviceSubclass=")+&device.b_device_sub_class.to_string())
.arg(String::from("idVendor=")+&device.id_vendor.to_string())
.arg(String::from("idProduct=")+&device.id_product.to_string())
.arg(String::from("iManufacturer=")+device.i_manufacturer)
.arg(String::from("iProduct=")+device.i_product)
.arg(String::from("iSerialNumber=")+device.i_serial_number)
.output().ok();
Command::new("mkdir").args(["-p","/tmp/ffs-mascon"]).output().ok();
Command::new("mount").args(["-t","functionfs","mascon","/tmp/ffs-mascon"]).output().ok();
thread::spawn(move || {
if let Ok(mut ep0) = File::open("/tmp/ffs-mascon/ep0") {
@ -70,9 +82,9 @@ fn init_gadget() {
}
});
if let Ok(mut ep0) = File::create("/tmp/ffs-mascon/ep0") {
ep0.write(&descriptors).ok();
ep0.write(descriptors).ok();
println!("USB Gadget: Descriptors written to EP0");
ep0.write(&strings).ok();
ep0.write(strings).ok();
println!("USB Gadget: Strings written to EP0");
}
}