From 4dd78b9727327843a02763edeb6228f240d52d3e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 26 Oct 2024 03:07:00 +0800 Subject: [PATCH] add descriptor chaining helper methods mass storage is...somewhat stable now, on both linux and windows, but there are some corrupt packets. need to look into whether electrical issue, or if we're stomping the transfer buffers because we've not managed them correctly. Also need to reflect on how to turn this API into something safer...the hardware is doing *so much* in this API, the driver is basically global mutable state with arbitrary slices you can write over the ends once you hand things off to the driver. --- libs/cramium-hal/src/usb/driver.rs | 60 +++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/libs/cramium-hal/src/usb/driver.rs b/libs/cramium-hal/src/usb/driver.rs index d3d33b689..18d580631 100644 --- a/libs/cramium-hal/src/usb/driver.rs +++ b/libs/cramium-hal/src/usb/driver.rs @@ -867,7 +867,10 @@ pub struct CorigineUsb { // can find a better place for it, but we need a spot that is accessible // via the interrupt handler. pub ms_state: UmsState, - pub ms_addr_len: Option<(usize, usize)>, + // addres, length tuples + pub callback_wr: Option<(usize, usize)>, + pub remaining_rd: Option<(usize, usize)>, + pub remaining_wr: Option<(usize, usize)>, } impl CorigineUsb { /// Safety: this function is generally pretty unsafe because the underlying hardware needs raw pointers, @@ -925,10 +928,57 @@ impl CorigineUsb { state: UsbDeviceState::NotAttached, cur_interface_num: 0, ms_state: UmsState::Idle, - ms_addr_len: None, + remaining_rd: None, + callback_wr: None, + remaining_wr: None, } } + pub fn setup_big_read(&mut self, app_buf: &mut [u8], disk: &[u8], offset: usize, length: usize) { + crate::println!( + "BIG READ offset {:x} len {:x} app_buf: {:x} disk: {:x?}", + offset, + length, + app_buf.as_ptr() as usize, + &disk[..length.min(8)] + ); + let actual_len = length.min(app_buf.len()); + let remaining_len = if length <= app_buf.len() { 0 } else { length - actual_len }; + if remaining_len > 0 { + self.remaining_rd = Some((offset + actual_len, remaining_len)); + } else { + self.remaining_rd = None; + } + app_buf[..actual_len].copy_from_slice(&disk[offset..offset + actual_len as usize]); + self.bulk_xfer(1, USB_SEND, app_buf.as_ptr() as usize, actual_len, 0, 0); + } + + pub fn setup_big_write( + &mut self, + app_buf_addr: usize, + app_buf_len: usize, + to_offset: usize, + total_length: usize, + ) { + crate::println!( + "BIG WRITE offset {:x} len {:x} app_buf: {:x} app_len: {:x}", + to_offset, + total_length, + app_buf_addr, + app_buf_len + ); + let actual_len = total_length.min(app_buf_len); + let remaining_len = if total_length <= app_buf_len { 0 } else { total_length - actual_len }; + if remaining_len > 0 { + self.remaining_wr = Some((to_offset + actual_len, remaining_len)); + } else { + self.remaining_wr = None; + } + + self.bulk_xfer(1, USB_RECV, app_buf_addr, actual_len, 0, 0); + self.callback_wr = Some((to_offset, actual_len)); + } + pub fn set_device_state(&mut self, state: UsbDeviceState) { self.state = state; } pub fn get_device_state(&self) -> UsbDeviceState { self.state } @@ -1987,12 +2037,12 @@ impl CorigineUsb { let mut pcs = udc_ep.pcs; crate::println!( - "PEI = {}, enq_pt = 0x{:x?}, bufaddr = 0x{:x} pcs = {} length = 0x{:x}", + "PEI = {} bufaddr = 0x{:x} pcs = {} length = 0x{:x}, enq_pt = 0x{:x?}", pei, - enq_pt, addr, udc_ep.pcs, - len + len, + enq_pt, ); for i in 0..num_trb { if num_trb == 1 {