diff --git a/services/usb-bao1x/src/hw.rs b/services/usb-bao1x/src/hw.rs index c09748a66..42841dd71 100644 --- a/services/usb-bao1x/src/hw.rs +++ b/services/usb-bao1x/src/hw.rs @@ -57,6 +57,7 @@ pub struct Bao1xUsb<'a> { // from the interrupt handler. pub double_lock: AtomicBool, pub led_state: KeyboardLedsReport, + pub irq_serviced: AtomicBool, } impl<'a> Bao1xUsb<'a> { @@ -122,6 +123,7 @@ impl<'a> Bao1xUsb<'a> { serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE], double_lock: AtomicBool::new(false), led_state: KeyboardLedsReport::default(), + irq_serviced: AtomicBool::new(false), } } @@ -355,6 +357,7 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) { while let Some(u2f_msg) = usb.fido_tx_queue.borrow_mut().pop_front() { u2f.write_report(&u2f_msg).ok(); } + usb.irq_serviced.store(true, Ordering::SeqCst); } Some(UsbIrqReq::KbdTx) => { let keyboard = composite.device::, _>(); diff --git a/services/usb-bao1x/src/main.rs b/services/usb-bao1x/src/main.rs index 0161066ac..0e3265095 100644 --- a/services/usb-bao1x/src/main.rs +++ b/services/usb-bao1x/src/main.rs @@ -441,6 +441,7 @@ pub(crate) fn main_hw() -> ! { 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 { + log::warn!("U2fTx: HANGUP"); u2f_ipc.code = U2fCode::Hangup; buffer.replace(u2f_ipc).unwrap(); continue; @@ -454,6 +455,15 @@ pub(crate) fn main_hw() -> ! { cu.fido_tx_queue.borrow_mut().push_back(u2f_msg); } cu.sw_irq(UsbIrqReq::FidoTx); + // ensure that the interrupt has happened, because the interrupt can create + // concurrency issues on the fido_tx_queue if we've re-entered this function + // before the interrupt has processed + while !cu.irq_serviced.load(Ordering::SeqCst) { + xous::yield_slice(); + } + // clear the serviced flag here - this is OK because this is the only other thread + // that can access the variable. + cu.irq_serviced.store(false, Ordering::SeqCst); log::debug!("enqueued U2F packet {:x?}", u2f_ipc.data); u2f_ipc.code = U2fCode::TxAck; } else {