diff --git a/Cargo.lock b/Cargo.lock index 4db1c2f57..ef3c37ddc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4482,6 +4482,7 @@ dependencies = [ "rkyv", "usb-device", "usbd-human-interface-device 0.2.1", + "usbd-serial", "usbd_mass_storage 0.1.0", "usbd_scsi 0.1.1", "utralib", @@ -4552,6 +4553,17 @@ dependencies = [ "usb-device", ] +[[package]] +name = "usbd-serial" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db75519b86287f12dcf0d171c7cf4ecc839149fe9f3b720ac4cfce52959e1dfe" +dependencies = [ + "embedded-hal", + "nb 0.1.3", + "usb-device", +] + [[package]] name = "usbd_bulk_only_transport" version = "0.1.0" diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index fb3163e0e..ebc7b6386 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -128,4 +128,5 @@ tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"] shellperf = ["ring", "perflib", "xous/v2p", "random-pick"] # this also needs the project-wide "perfcounter" feature to be selected. shellchat is the exclusive manager of the performance counter, do not use with e.g. "vaultperf" feature extra-tests = [] mass-storage = [] -default = [] # "debugprint" +serial = [] +default = ["serial"] # "debugprint" diff --git a/services/shellchat/src/cmds/usb.rs b/services/shellchat/src/cmds/usb.rs index e5e7135e7..ed8aa8ab7 100644 --- a/services/shellchat/src/cmds/usb.rs +++ b/services/shellchat/src/cmds/usb.rs @@ -45,9 +45,15 @@ impl<'a> ShellCmdApi<'a> for Usb { self.usb_dev.debug_usb(Some(false)).unwrap(); write!(ret, "USB connected to Debug core, secrets readable!").unwrap(); } + #[cfg(feature="serial")] + "serial" => { + self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::Serial).unwrap(); + write!(ret, "USB connected to serial core").unwrap(); + } "send" => { match self.usb_dev.get_current_core() { - Ok(UsbDeviceType::FidoKbd) => { + Ok(UsbDeviceType::FidoKbd) + | Ok(UsbDeviceType::Serial) => { let mut val = String::new(); join_tokens(&mut val, &mut tokens); match self.usb_dev.send_str(&val) { diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index c9d8c86eb..c6126ee0f 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -34,6 +34,9 @@ num_enum = { version = "0.5", default-features = false } # used by the usbd-huma usbd_scsi = { path = "../../libs/mass-storage/usbd_scsi", optional = true } usbd_mass_storage = { path = "../../libs/mass-storage/usbd_mass_storage", optional = true } +# Serial support +usbd-serial = "0.1.1" + ghostfat = "0.5.0" [dependencies.usb-device] @@ -51,5 +54,6 @@ hosted = ["utralib/hosted"] renode = ["utralib/renode"] minimal = ["mass-storage"] mass-storage = ["usbd_scsi", "usbd_mass_storage"] +serial = [] mjolnir = [] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care. -default = [] +default = ["serial"] diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index dde347414..51c5a6677 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -83,6 +83,8 @@ pub enum UsbDeviceType { Fido = 2, #[cfg(feature="mass-storage")] MassStorage = 3, + #[cfg(feature="serial")] + Serial = 4, } use std::convert::TryFrom; impl TryFrom for UsbDeviceType { @@ -95,6 +97,8 @@ impl TryFrom for UsbDeviceType { 2 => Ok(UsbDeviceType::Fido), #[cfg(feature="mass-storage")] 3 => Ok(UsbDeviceType::MassStorage), + #[cfg(feature="serial")] + 4 => Ok(UsbDeviceType::Serial), _ => Err("Invalid UsbDeviceType specifier"), } } diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index aa3103fd9..343ba23a1 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -131,6 +131,10 @@ impl UsbHid { 0 => Ok(UsbDeviceType::Debug), 1 => Ok(UsbDeviceType::FidoKbd), 2 => Ok(UsbDeviceType::Fido), + #[cfg(feature="mass-storage")] + 3 => Ok(UsbDeviceType::MassStorage), + #[cfg(feature="serial")] + 4 => Ok(UsbDeviceType::Serial), _ => Err(xous::Error::InternalError) } } diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 31e3df9f7..e92b1f2bc 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -27,6 +27,9 @@ use keyboard::KeyMap; use xous_ipc::Buffer; use std::collections::VecDeque; +#[cfg(feature="serial")] +use usbd_serial::SerialPort; + pub struct EmbeddedClock { start: std::time::Instant, } @@ -55,6 +58,8 @@ enum Views { FidoOnly = 1, #[cfg(feature="mass-storage")] MassStorage = 2, + #[cfg(feature="serial")] + Serial = 3, } #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] @@ -174,6 +179,8 @@ pub(crate) fn main_hw() -> ! { // do the same thing for mass storage #[cfg(feature="mass-storage")] let ums_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + #[cfg(feature="serial")] + let serial_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); // track which view is visible on the device core #[cfg(not(feature="minimal"))] @@ -247,6 +254,23 @@ pub(crate) fn main_hw() -> ! { .max_power(500) .build(); + // Serial + #[cfg(feature="serial")] + const SERIAL_BUF_LEN: usize = 1024; + #[cfg(feature="serial")] + let serial_alloc = UsbBusAllocator::new(serial_dev); + #[cfg(feature="serial")] + // this will create a default port with 128 bytes of backing store + let mut serial_port = SerialPort::new(&serial_alloc); + #[cfg(feature="serial")] + let mut serial_device = UsbDeviceBuilder::new(&serial_alloc, UsbVidPid(0x1209, 0x3613)) + .manufacturer("Kosagi") + .product("Precursor") + .serial_number(&serial_number) + .self_powered(false) + .max_power(500) + .build(); + let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default(); let mut fido_listener: Option = None; // under the theory that PIDs cannot be forged. TODO: check that PIDs cannot be forged. @@ -356,7 +380,7 @@ pub(crate) fn main_hw() -> ! { #[cfg(feature="mass-storage")] Some(Opcode::SetBlockDevice) => msg_blocking_scalar_unpack!(msg, read_id, write_id, max_lba_id, _, { xous::send_message( - abdcid, + abdcid, xous::Message::new_blocking_scalar( apps_block_device::BlockDeviceMgmtOp::SetOps.to_usize().unwrap(), read_id, write_id, max_lba_id, 0, ) @@ -407,6 +431,13 @@ pub(crate) fn main_hw() -> ! { _ => () }; } + #[cfg(feature="serial")] + Views::Serial => { + match serial_device.force_reset() { + Err(e) => log::warn!("USB reset on resume failed: {:?}", e), + _ => () + } + } } // resume2 brings us to our last application state usbmgmt.xous_resume2(); @@ -489,7 +520,9 @@ pub(crate) fn main_hw() -> ! { Views::FidoWithKbd => composite.interface::, _>(), Views::FidoOnly => fido_class.interface::, _>(), #[cfg(feature="mass-storage")] - Views::MassStorage => panic!("expected u2f tx when in mass storage mode!"), + Views::MassStorage => panic!("did not expect u2f tx when in mass storage mode!"), + #[cfg(feature="serial")] + Views::Serial => panic!("did not expect u2f tx while in serial mode!"), }; u2f.write_report(&u2f_msg).ok(); log::debug!("sent U2F packet {:x?}", u2f_ipc.data); @@ -529,7 +562,23 @@ pub(crate) fn main_hw() -> ! { log::debug!("ums device had something to do!") } None - } + }, + #[cfg(feature="serial")] + Views::Serial => { + if serial_device.poll(&mut [&mut serial_port]) { + let mut data = [0u8; SERIAL_BUF_LEN]; + match serial_port.read(&mut data) { + Ok(len) => { + log::info!("Serial got data: {:x?}", &data[..len]); + } + Err(e) => { + log::info!("Serial read error: {:?}", e); + } + } + // TODO: add a listener here and report read data to the listener + } + None + }, }; if let Some(u2f) = maybe_u2f { match u2f.read_report() { @@ -559,6 +608,8 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => fido_dev.state() == UsbDeviceState::Suspend, #[cfg(feature="mass-storage")] Views::MassStorage => ums_device.state() == UsbDeviceState::Suspend, + #[cfg(feature="serial")] + Views::Serial => serial_device.state() == UsbDeviceState::Suspend, }; if is_suspend { log::info!("suspend detected"); @@ -634,6 +685,21 @@ pub(crate) fn main_hw() -> ! { } } } + #[cfg(feature="serial")] + UsbDeviceType::Serial => { + log::info!("Connecting device serial"); + match view { + Views::Serial => usbmgmt.connect_device_core(true), + _ => { + view = Views::Serial; + usbmgmt.ll_reset(true); + tt.sleep_ms(1000).ok(); + usbmgmt.ll_connect_device_core(true); + tt.sleep_ms(EXTENDED_CORE_RESET_MS).ok(); + usbmgmt.ll_reset(false); + } + } + } } xous::return_scalar(msg.sender, 0).unwrap(); }), @@ -705,6 +771,23 @@ pub(crate) fn main_hw() -> ! { } } } + #[cfg(feature="serial")] + UsbDeviceType::Serial => { + log::info!("Ensuring serial device"); + if !usbmgmt.is_device_connected() { + view = Views::Serial; + usbmgmt.connect_device_core(true); + } else { + if view != Views::Serial { + view = Views::Serial; + usbmgmt.ll_reset(true); + tt.sleep_ms(1000).ok(); + usbmgmt.ll_connect_device_core(true); + tt.sleep_ms(EXTENDED_CORE_RESET_MS).ok(); + usbmgmt.ll_reset(false); + } + } + } } xous::return_scalar(msg.sender, 0).unwrap(); }), @@ -715,6 +798,8 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => xous::return_scalar(msg.sender, UsbDeviceType::Fido as usize).unwrap(), #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, UsbDeviceType::MassStorage as usize).unwrap(), + #[cfg(feature="serial")] + Views::Serial => xous::return_scalar(msg.sender, UsbDeviceType::Serial as usize).unwrap(), } } else { xous::return_scalar(msg.sender, UsbDeviceType::Debug as usize).unwrap(); @@ -770,6 +855,8 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => xous::return_scalar(msg.sender, fido_dev.state() as usize).unwrap(), #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, ums_device.state() as usize).unwrap(), + #[cfg(feature="serial")] + Views::Serial => xous::return_scalar(msg.sender, serial_device.state() as usize).unwrap(), } }), Some(Opcode::SendKeyCode) => msg_blocking_scalar_unpack!(msg, code0, code1, code2, autoup, { @@ -832,9 +919,34 @@ pub(crate) fn main_hw() -> ! { sent += 1; } } + Views::Serial => { + // this is implemented as a "blocking write": the routine will block until the data has all been written. + let send_data = usb_send.s.as_bytes(); + let to_send = usb_send.s.len(); + log::debug!("serial RTS: {:?}", serial_port.rts()); + log::debug!("serial DTR: {:?}", serial_port.dtr()); + while sent < to_send { + match serial_port.write(&send_data[sent..to_send]) { + Ok(written) => { + sent += written; + } + Err(_) => { + log::warn!("Serial send is blocking. Delaying and trying again."); + tt.sleep_ms(100).ok(); + } + } + match serial_port.flush() { + Ok(_) => {}, + Err(_) => { + log::warn!("Serial port reported WouldBlock on flush"); + tt.sleep_ms(100).ok(); + } + } + } + } _ => {} // do nothing; will report that 0 characters were sent } - usb_send.sent = Some(sent); + usb_send.sent = Some(sent as _); buffer.replace(usb_send).unwrap(); } Some(Opcode::GetLedState) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {