From 501f7a4d5f06d4018e1a4d83bd98e74b17bb557b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 16 Jul 2023 17:35:24 +0800 Subject: [PATCH] appy deferred-init idiom to usb-device. NOTE: box not applied Box can't be applied to this crate because of interoperability issues with the USB driver crate when it is applied. Still, it seems to "work" because deferred-init pins down the address of the hardware management structure (for now). --- services/usb-device-xous/src/hw.rs | 19 +++++++++---------- services/usb-device-xous/src/main_hw.rs | 11 +++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/services/usb-device-xous/src/hw.rs b/services/usb-device-xous/src/hw.rs index 0a3e439f9..f76fa761f 100644 --- a/services/usb-device-xous/src/hw.rs +++ b/services/usb-device-xous/src/hw.rs @@ -219,7 +219,7 @@ pub struct SpinalUsbDevice { } impl SpinalUsbDevice { pub fn new(sid: xous::SID, usb: MemoryRange, csr: MemoryRange) -> SpinalUsbDevice { - let mut usbdev = SpinalUsbDevice { + SpinalUsbDevice { conn: xous::connect(sid).unwrap(), csr_addr: csr.as_ptr() as u32, csr: AtomicCsr::new(csr.as_mut_ptr() as *mut u32), @@ -236,24 +236,23 @@ impl SpinalUsbDevice { address: AtomicUsize::new(0), read_allowed: AtomicU16::new(0), last_wr_desc: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])), - }; - + } + } + pub fn init(&self) { if !INTERRUPT_INIT_DONE.fetch_or(true, Ordering::SeqCst) { xous::claim_interrupt( utra::usbdev::USBDEV_IRQ, handle_usb, - (&mut usbdev) as *mut SpinalUsbDevice as *mut usize, + self as *const SpinalUsbDevice as *mut usize, ) .expect("couldn't claim irq"); - let p = usbdev.csr.r(utra::usbdev::EV_PENDING); - usbdev.csr.wo(utra::usbdev::EV_PENDING, p); // clear in case it's pending for some reason - usbdev.csr.wfo(utra::usbdev::EV_ENABLE_USB, 1); + let p = self.csr.r(utra::usbdev::EV_PENDING); + self.csr.wo(utra::usbdev::EV_PENDING, p); // clear in case it's pending for some reason + self.csr.wfo(utra::usbdev::EV_ENABLE_USB, 1); } let mut cfg = UdcConfig(0); cfg.set_pullup_on(true); // required for proper operation - usbdev.regs.set_config(cfg); - - usbdev + self.regs.set_config(cfg); } pub fn get_iface(&self) -> SpinalUsbMgmt { SpinalUsbMgmt { diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 22663be13..636d8ae36 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -192,15 +192,26 @@ pub(crate) fn main_hw() -> ! { ) .expect("couldn't map USB CSR range"); + // Notes: + // - Most drivers would `Box()` the hardware management structure to make sure the compiler doesn't + // move its location. However, we can't do this here because we are trying to maintain compatibility + // with another crate that implements the USB stack which can't handle Box'd structures. + // - It is safe to call `.init()` repeatedly because within `init()` we have an atomic bool that + // tracks if the interrupt handler has been hooked, and ignores further requests to hook it. let usb_fidokbd_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + usb_fidokbd_dev.init(); let mut usbmgmt = usb_fidokbd_dev.get_iface(); // before doing any allocs, clone a copy of the hardware access structure so we can build a second // view into the hardware with only FIDO descriptors let usb_fido_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + usb_fido_dev.init(); // do the same thing for mass storage #[cfg(feature="mass-storage")] let ums_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + #[cfg(feature="mass-storage")] + ums_dev.init(); let serial_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + serial_dev.init(); // track which view is visible on the device core #[cfg(not(feature="minimal"))]