diff --git a/services/usb-bao1x/src/api.rs b/services/usb-bao1x/src/api.rs index 8d79fb6de..5147c9c07 100644 --- a/services/usb-bao1x/src/api.rs +++ b/services/usb-bao1x/src/api.rs @@ -4,7 +4,7 @@ pub(crate) const SERVER_NAME_USB_DEVICE: &'static str = "_Xous USB device driver_"; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum Opcode { +pub enum Opcode { /// Returns the link status LinkStatus = 0, /// Send a keyboard code diff --git a/services/usb-bao1x/src/hw.rs b/services/usb-bao1x/src/hw.rs index ab169c68d..d3b158091 100644 --- a/services/usb-bao1x/src/hw.rs +++ b/services/usb-bao1x/src/hw.rs @@ -162,7 +162,7 @@ impl<'a> Bao1xUsb<'a> { /// Process an unplug event - only valid on baosec, because dabao doesn't have a battery and unplugging /// it would power it down. - #[cfg(all(feature = "board-baosec", not(feature = "oem-baosec-lite")))] + #[cfg(all(feature = "board-baosec"))] pub fn unplug(&mut self) { // disable all interrupts so we can safely go through initialization routines self.irq_csr.wo(utra::irqarray1::EV_ENABLE, 0); diff --git a/services/usb-bao1x/src/lib.rs b/services/usb-bao1x/src/lib.rs index 85b841878..15d166259 100644 --- a/services/usb-bao1x/src/lib.rs +++ b/services/usb-bao1x/src/lib.rs @@ -364,6 +364,8 @@ impl UsbHid { ) .unwrap(); } + + pub fn cid(&self) -> xous::CID { self.conn } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/usb-bao1x/src/main.rs b/services/usb-bao1x/src/main.rs index cafc1360c..bd86eb26c 100644 --- a/services/usb-bao1x/src/main.rs +++ b/services/usb-bao1x/src/main.rs @@ -309,6 +309,16 @@ pub(crate) fn main_hw() -> ! { // log::warn!("Received an interrupt but no actual event reported"); } }, + #[cfg(all(feature = "board-baosec", feature = "oem-baosec-lite"))] + Opcode::PmicIrq => { + use bao1x_hal::axp2101::VbusIrq; + if let Some(scalar) = msg.body.scalar_message_mut() { + let vbus_irq = VbusIrq::from(scalar.arg1); + if vbus_irq == VbusIrq::Remove { + cu.unplug(); + } + } + } Opcode::U2fRxDeferred => { // notify the event listener, if any if observer_conn.is_some() && observer_op.is_some() { @@ -432,6 +442,11 @@ pub(crate) fn main_hw() -> ! { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut u2f_ipc = buffer.to_original::().unwrap(); + if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + u2f_ipc.code = U2fCode::Hangup; + buffer.replace(u2f_ipc).unwrap(); + continue; + } if fido_listener_pid == msg.sender.pid() { let mut u2f_msg = RawFidoReport::default(); assert_eq!(u2f_ipc.code, U2fCode::Tx, "Expected U2fCode::Tx in wrapper"); @@ -450,6 +465,10 @@ pub(crate) fn main_hw() -> ! { } Opcode::SendKeyCode => { if let Some(scalar) = msg.body.scalar_message_mut() { + if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + continue; + } + let code0 = scalar.arg1; let code1 = scalar.arg2; let code2 = scalar.arg3; @@ -493,6 +512,12 @@ pub(crate) fn main_hw() -> ! { unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut usb_send = buffer.to_original::().unwrap(); let mut sent = 0; + if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + log::warn!("Aborting send, no USB configured"); + usb_send.sent = Some(0); + buffer.replace(usb_send).unwrap(); + continue; + } // check keymap on every call because we may need to toggle this for e.g. plugging // into a new host with a different map @@ -696,6 +721,9 @@ pub(crate) fn main_hw() -> ! { serial_listener.take(); } Opcode::SerialFlush => msg_scalar_unpack!(msg, _, _, _, _, { + if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + continue; + } // this will hardware flush any pending items in usb_serial driver cu.serial_port.flush().ok(); // this tries to return any data that's pending within the main loop's buffers @@ -738,6 +766,9 @@ pub(crate) fn main_hw() -> ! { } }), Opcode::SerialSendData => { + if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + continue; + } let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let data = buffer.to_original::().unwrap(); let mut total_sent = 0; @@ -791,7 +822,7 @@ pub(crate) fn main_hw() -> ! { break; } _ => { - unimplemented!( + log::warn!( "Opcode {:?} not implemented for this version of the USB stack: {:?}", opcode, msg