remove mass storage from baremetal

No need for it in the baremetal environment, and it frees up
a lot of RAM for use in baremetal applications.
This commit is contained in:
bunnie 2025-10-01 01:27:40 +08:00
parent ca7afb63b6
commit b0378d346c
5 changed files with 21 additions and 1067 deletions

View file

@ -91,7 +91,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
let (mut last_usb_state, mut portsc) = crate::platform::usb::glue::hotplug_usb(&iox);
crate::println!(
"usb connected {:?}, tx ready {:?}",
" [usb connected {:?}] [tx idle {:?}]",
USB_CONNECTED.load(Ordering::SeqCst),
crate::platform::usb::TX_IDLE.load(Ordering::SeqCst)
);

View file

@ -20,25 +20,6 @@ pub unsafe fn init_usb() {
};
usb.assign_handler(handle_event);
// initialize the "disk" area
let disk = handlers::conjure_disk();
disk.fill(0);
// setup MBR
disk[..MBR_TEMPLATE.len()].copy_from_slice(&MBR_TEMPLATE);
//set storage size
disk[0x20..0x24].copy_from_slice(&(RAMDISK_LEN as u32 / SECTOR_SIZE as u32).to_le_bytes());
// populate an initial "windows-friendly" FAT layout, generated by plugging the empty disk
// into windows and letting it...do its thing, then snapshotting the data.
disk[TABLE_OFFSET_FAT1..TABLE_OFFSET_FAT1 + FAT_TABLE.len()].copy_from_slice(&FAT_TABLE);
disk[TABLE_OFFSET_FAT2..TABLE_OFFSET_FAT2 + FAT_TABLE.len()].copy_from_slice(&FAT_TABLE);
disk[ROOT_DIR_OFFSET..ROOT_DIR_OFFSET + ROOT_DIR.len()].copy_from_slice(&ROOT_DIR);
disk[FAT_DATA1_OFFSET..FAT_DATA1_OFFSET + FAT_DATA1.len()].copy_from_slice(&FAT_DATA1);
disk[FAT_DATA2_OFFSET..FAT_DATA2_OFFSET + FAT_DATA2.len()].copy_from_slice(&FAT_DATA2);
disk[FAT_DATA3_OFFSET..FAT_DATA3_OFFSET + FAT_DATA3.len()].copy_from_slice(&FAT_DATA3);
// install the interrupt handler
enable_irq(utralib::utra::irqarray1::IRQARRAY1_IRQ);
// for testing
@ -51,46 +32,6 @@ pub unsafe fn init_usb() {
}
}
#[allow(dead_code)]
pub unsafe fn test_usb() {
if let Some(ref mut usb_ref) = USB {
let usb = &mut *core::ptr::addr_of_mut!(*usb_ref);
usb.reset();
let mut poweron = 0;
loop {
usb.udc_handle_interrupt();
if usb.pp() {
poweron += 1; // .pp() is a sham. MPW has no way to tell if power is applied. This needs to be fixed for bao1x.
}
delay(100);
if poweron >= 4 {
break;
}
}
usb.reset();
usb.init();
usb.start();
usb.update_current_speed();
// IRQ enable must happen without dependency on the hardware lock
usb.irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, 0xffff_ffff); // blanket clear
usb.irq_csr.wfo(utralib::utra::irqarray1::EV_ENABLE_USBC_DUPE, 1);
crate::println_d!("hw started...");
let mut i = 0;
loop {
// wait for interrupt handler to do something
crate::println_d!("{}", i);
i += 1;
delay(10_000);
// for testing interrupt handler
let mut irqarray19 = utralib::CSR::new(utralib::utra::irqarray19::HW_IRQARRAY19_BASE as *mut u32);
irqarray19.wfo(utralib::utra::irqarray19::EV_SOFT_TRIGGER, 0x80);
}
} else {
crate::println_d!("USB core not allocated, skipping USB test");
}
}
fn delay(quantum: usize) {
use utralib::{CSR, utra};
// abuse the d11ctime timer to create some time-out like thing
@ -149,7 +90,7 @@ fn get_status_request(this: &mut CorigineUsb, request_type: u8, index: u16) {
fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
// crate::println_d!("handle_event: {:x?}", event_trb);
let pei = event_trb.get_endpoint_id();
let ep_num = pei >> 1;
let _ep_num = pei >> 1;
let udc_ep = &mut this.udc_ep[pei as usize];
let mut ret = CrgEvent::None;
match event_trb.get_trb_type() {
@ -275,12 +216,6 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
delay(100);
/* do set sel */
crate::println_d!("SEL_VALUE NOT HANDLED");
/*
crg_udc->sel_value.u1_sel_value = *ep0_buf;
crg_udc->sel_value.u1_pel_value = *(ep0_buf+1);
crg_udc->sel_value.u2_sel_value = *(uint16_t*)(ep0_buf+2);
crg_udc->sel_value.u2_pel_value = *(uint16_t*)(ep0_buf+4);
*/
}
USB_REQ_SET_ISOCH_DELAY => {
crate::println_d!("USB_REQ_SET_ISOCH_DELAY");
@ -290,20 +225,12 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
USB_REQ_CLEAR_FEATURE => {
crate::println_d!("USB_REQ_CLEAR_FEATURE");
crate::println_d!("*** UNSUPPORTED ***");
/* do clear feature */
// clear_feature_request(setup_pkt.b_request_type, w_index, w_value);
this.ep_halt(0, USB_RECV);
}
USB_REQ_SET_FEATURE => {
crate::println_d!("USB_REQ_SET_FEATURE\r\n");
crate::println_d!("*** UNSUPPORTED ***");
/* do set feature */
/*
if crg_udc_get_device_state() == USB_STATE_CONFIGURED {
set_feature_request(setup_pkt.b_request_type, w_index, w_value);
} else {
crg_udc_ep_halt(0, USB_RECV);
}
*/
this.ep_halt(0, USB_RECV);
}
USB_REQ_SET_CONFIGURATION => {
let mut pass = false;
@ -317,18 +244,11 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
}
if !pass {
// MSD as before
this.assign_completion_handler(usb_ep1_bulk_in_complete, 1, USB_SEND);
this.assign_completion_handler(usb_ep1_bulk_out_complete, 1, USB_RECV);
// USB-CDC-ACM
this.assign_completion_handler(usb_ep3_bulk_in_complete, 3, USB_SEND);
this.assign_completion_handler(usb_ep3_bulk_out_complete, 3, USB_RECV);
this.assign_completion_handler(usb_ep2_int_in_complete, 2, USB_SEND);
enable_composite_eps(this);
// Kick MSD CBW receive as before
this.bulk_xfer(1, USB_RECV, this.cbw_ptr(), 31, 0, 0);
this.ms_state = UmsState::CommandPhase;
enable_serial_eps(this);
// Kick off CDC-ACM
let acm_buf_rx = this.cdc_acm_rx_slice();
@ -384,39 +304,6 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
}
} else if (setup_pkt.b_request_type & USB_TYPE_MASK) == USB_TYPE_CLASS {
match setup_pkt.b_request {
0xff => {
crate::println_d!("Mass Storage Reset\r\n");
if (0 == w_value)
&& (InterfaceDescriptor::default_mass_storage().b_interface_number
== w_index as u8)
&& (0 == w_length)
{
this.ep_unhalt(1, USB_SEND);
this.ep_unhalt(1, USB_RECV);
enable_mass_storage_eps(this, ep_num);
this.ms_state = UmsState::CommandPhase;
this.bulk_xfer(1, USB_RECV, this.cbw_ptr(), 31, 0, 0);
//crg_udc_ep0_status(false,0);
this.ep0_send(0, 0, 0);
} else {
this.ep_halt(0, USB_RECV);
}
}
0xfe => {
crate::println_d!("Get Max LUN");
if w_index != 0 || w_value != 0 || w_length != 1 {
this.ep_halt(0, USB_RECV);
} else {
let ep0_buf = unsafe {
core::slice::from_raw_parts_mut(
this.ep0_buf.load(Ordering::SeqCst) as *mut u8,
CRG_UDC_EP0_REQBUFSIZE,
)
};
ep0_buf[0] = 0;
this.ep0_send(ep0_buf.as_ptr() as usize, 1, 0);
}
}
0x20 => {
// SET_LINE_CODING (host -> device, 7 bytes)
crate::println_d!("CDC SET_LINE_CODING");

View file

@ -1,7 +1,7 @@
use bao1x_api::{IoGpio, IoSetup};
use bao1x_hal::usb::driver::UsbDeviceState;
use crate::{glue, usb};
use crate::glue;
// Empirically measured PORTSC when the port is unplugged. This might be a brittle way
// to detect if the device is unplugged.
@ -11,12 +11,6 @@ const DISCONNECT_STATE_HS: u32 = 0xc6b; // 11_0_0011_0_1_01_1
pub fn is_disconnected(state: u32) -> bool { state == DISCONNECT_STATE_HS || state == DISCONNECT_STATE }
pub fn setup() -> (UsbDeviceState, u32) {
crate::println_d!(
"RAM disk starts at {:x} and is {}kiB in length",
usb::RAMDISK_ADDRESS,
usb::RAMDISK_LEN / 1024
);
// safety: this is safe because we're calling this before any access to `USB` static mut
// state, and we also understand that the .data section doesn't exist in the loader and
// we've taken countermeasures to initialize everything "from code", i.e. not relying

View file

@ -7,131 +7,8 @@ use super::*;
pub static TX_IDLE: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(true);
// Locate the "disk"
pub(crate) const RAMDISK_ADDRESS: usize = crate::platform::HEAP_START + crate::platform::HEAP_LEN;
pub(crate) const STACK_SIZE: usize = 128 * 1024; // 128k for stack is enough? maybe? if the ramdisk overflows, it smashes stack - dangerous!
pub(crate) const RAMDISK_LEN: usize =
crate::platform::RAM_SIZE - (RAMDISK_ADDRESS - crate::platform::RAM_BASE) - STACK_SIZE;
pub(crate) const SECTOR_SIZE: u16 = 512;
// MBR template
// 0x0b~0x0C 2 bytes means block size, default 0x200 bytes
// 0x20~0x23 4 bytes means block number, default 0x400 block
#[rustfmt::skip] // keep this in 16-byte width
pub(crate) const MBR_TEMPLATE: [u8; SECTOR_SIZE as usize] = [
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x20, 0x06, 0x00,
0x02, 0x00, 0x02, 0x00, 0x0C, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0xBB, 0xBF, 0xB7, 0xB0, 0x4E, 0x4F, 0x20, 0x4E, 0x41,
0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x33, 0xC9,
0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E, 0xD9, 0xB8, 0x00, 0x20, 0x8E, 0xC0, 0xFC, 0xBD, 0x00, 0x7C,
0x38, 0x4E, 0x24, 0x7D, 0x24, 0x8B, 0xC1, 0x99, 0xE8, 0x3C, 0x01, 0x72, 0x1C, 0x83, 0xEB, 0x3A,
0x66, 0xA1, 0x1C, 0x7C, 0x26, 0x66, 0x3B, 0x07, 0x26, 0x8A, 0x57, 0xFC, 0x75, 0x06, 0x80, 0xCA,
0x02, 0x88, 0x56, 0x02, 0x80, 0xC3, 0x10, 0x73, 0xEB, 0x33, 0xC9, 0x8A, 0x46, 0x10, 0x98, 0xF7,
0x66, 0x16, 0x03, 0x46, 0x1C, 0x13, 0x56, 0x1E, 0x03, 0x46, 0x0E, 0x13, 0xD1, 0x8B, 0x76, 0x11,
0x60, 0x89, 0x46, 0xFC, 0x89, 0x56, 0xFE, 0xB8, 0x20, 0x00, 0xF7, 0xE6, 0x8B, 0x5E, 0x0B, 0x03,
0xC3, 0x48, 0xF7, 0xF3, 0x01, 0x46, 0xFC, 0x11, 0x4E, 0xFE, 0x61, 0xBF, 0x00, 0x00, 0xE8, 0xE6,
0x00, 0x72, 0x39, 0x26, 0x38, 0x2D, 0x74, 0x17, 0x60, 0xB1, 0x0B, 0xBE, 0xA1, 0x7D, 0xF3, 0xA6,
0x61, 0x74, 0x32, 0x4E, 0x74, 0x09, 0x83, 0xC7, 0x20, 0x3B, 0xFB, 0x72, 0xE6, 0xEB, 0xDC, 0xA0,
0xFB, 0x7D, 0xB4, 0x7D, 0x8B, 0xF0, 0xAC, 0x98, 0x40, 0x74, 0x0C, 0x48, 0x74, 0x13, 0xB4, 0x0E,
0xBB, 0x07, 0x00, 0xCD, 0x10, 0xEB, 0xEF, 0xA0, 0xFD, 0x7D, 0xEB, 0xE6, 0xA0, 0xFC, 0x7D, 0xEB,
0xE1, 0xCD, 0x16, 0xCD, 0x19, 0x26, 0x8B, 0x55, 0x1A, 0x52, 0xB0, 0x01, 0xBB, 0x00, 0x00, 0xE8,
0x3B, 0x00, 0x72, 0xE8, 0x5B, 0x8A, 0x56, 0x24, 0xBE, 0x0B, 0x7C, 0x8B, 0xFC, 0xC7, 0x46, 0xF0,
0x3D, 0x7D, 0xC7, 0x46, 0xF4, 0x29, 0x7D, 0x8C, 0xD9, 0x89, 0x4E, 0xF2, 0x89, 0x4E, 0xF6, 0xC6,
0x06, 0x96, 0x7D, 0xCB, 0xEA, 0x03, 0x00, 0x00, 0x20, 0x0F, 0xB6, 0xC8, 0x66, 0x8B, 0x46, 0xF8,
0x66, 0x03, 0x46, 0x1C, 0x66, 0x8B, 0xD0, 0x66, 0xC1, 0xEA, 0x10, 0xEB, 0x5E, 0x0F, 0xB6, 0xC8,
0x4A, 0x4A, 0x8A, 0x46, 0x0D, 0x32, 0xE4, 0xF7, 0xE2, 0x03, 0x46, 0xFC, 0x13, 0x56, 0xFE, 0xEB,
0x4A, 0x52, 0x50, 0x06, 0x53, 0x6A, 0x01, 0x6A, 0x10, 0x91, 0x8B, 0x46, 0x18, 0x96, 0x92, 0x33,
0xD2, 0xF7, 0xF6, 0x91, 0xF7, 0xF6, 0x42, 0x87, 0xCA, 0xF7, 0x76, 0x1A, 0x8A, 0xF2, 0x8A, 0xE8,
0xC0, 0xCC, 0x02, 0x0A, 0xCC, 0xB8, 0x01, 0x02, 0x80, 0x7E, 0x02, 0x0E, 0x75, 0x04, 0xB4, 0x42,
0x8B, 0xF4, 0x8A, 0x56, 0x24, 0xCD, 0x13, 0x61, 0x61, 0x72, 0x0B, 0x40, 0x75, 0x01, 0x42, 0x03,
0x5E, 0x0B, 0x49, 0x75, 0x06, 0xF8, 0xC3, 0x41, 0xBB, 0x00, 0x00, 0x60, 0x66, 0x6A, 0x00, 0xEB,
0xB0, 0x42, 0x4F, 0x4F, 0x54, 0x4D, 0x47, 0x52, 0x20, 0x20, 0x20, 0x20, 0x0D, 0x0A, 0x52, 0x65,
0x6D, 0x6F, 0x76, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6B, 0x73, 0x20, 0x6F, 0x72, 0x20, 0x6F, 0x74,
0x68, 0x65, 0x72, 0x20, 0x6D, 0x65, 0x64, 0x69, 0x61, 0x2E, 0xFF, 0x0D, 0x0A, 0x44, 0x69, 0x73,
0x6B, 0x20, 0x65, 0x72, 0x72, 0x6F, 0x72, 0xFF, 0x0D, 0x0A, 0x50, 0x72, 0x65, 0x73, 0x73, 0x20,
0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, 0x20, 0x72, 0x65, 0x73, 0x74, 0x61,
0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAC, 0xCB, 0xD8, 0x55, 0xAA,
];
pub(crate) const FAT_TABLE: [u8; 0x08] = [0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F];
pub(crate) const TABLE_OFFSET_FAT1: usize = 0xC00;
pub(crate) const TABLE_OFFSET_FAT2: usize = 0xE00;
#[rustfmt::skip] // keep this in 16-byte width
pub (crate) const ROOT_DIR: [u8; 0x80] = [
0x42, 0x41, 0x4F, 0x53, 0x45, 0x43, 0x20, 0x20, 0x20, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCD, 0x78, 0x74, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x20, 0x00, 0x49, 0x00, 0x6E, 0x00, 0x66, 0x00, 0x6F, 0x00, 0x0F, 0x00, 0x72, 0x72, 0x00,
0x6D, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00,
0x01, 0x53, 0x00, 0x79, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x0F, 0x00, 0x72, 0x6D, 0x00,
0x20, 0x00, 0x56, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x65, 0x00,
0x53, 0x59, 0x53, 0x54, 0x45, 0x4D, 0x7E, 0x31, 0x20, 0x20, 0x20, 0x16, 0x00, 0x36, 0xCF, 0x78,
0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
];
pub(crate) const ROOT_DIR_OFFSET: usize = 0x1000;
#[rustfmt::skip] // keep this in 16-byte width
pub (crate) const FAT_DATA1: [u8; 0x100] = [
0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x36, 0xCF, 0x78,
0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x2E, 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x36, 0xCF, 0x78,
0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x47, 0x00, 0x75, 0x00, 0x69, 0x00, 0x64, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0x01, 0x49, 0x00, 0x6E, 0x00, 0x64, 0x00, 0x65, 0x00, 0x78, 0x00, 0x0F, 0x00, 0xFF, 0x65, 0x00,
0x72, 0x00, 0x56, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x65, 0x00,
0x49, 0x4E, 0x44, 0x45, 0x58, 0x45, 0x7E, 0x31, 0x20, 0x20, 0x20, 0x20, 0x00, 0x4E, 0xCF, 0x78,
0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x03, 0x00, 0x4C, 0x00, 0x00, 0x00,
0x42, 0x74, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0xCE, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0x01, 0x57, 0x00, 0x50, 0x00, 0x53, 0x00, 0x65, 0x00, 0x74, 0x00, 0x0F, 0x00, 0xCE, 0x74, 0x00,
0x69, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x73, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x64, 0x00, 0x61, 0x00,
0x57, 0x50, 0x53, 0x45, 0x54, 0x54, 0x7E, 0x31, 0x44, 0x41, 0x54, 0x20, 0x00, 0xB5, 0xD8, 0x78,
0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD9, 0x78, 0x74, 0x59, 0x04, 0x00, 0x0C, 0x00, 0x00, 0x00,
];
pub(crate) const FAT_DATA1_OFFSET: usize = 0x5000;
#[rustfmt::skip] // keep this in 16-byte width
pub(crate) const FAT_DATA2: [u8; 0x50] = [
0x7B, 0x00, 0x34, 0x00, 0x33, 0x00, 0x39, 0x00, 0x44, 0x00, 0x41, 0x00, 0x31, 0x00, 0x32, 0x00,
0x44, 0x00, 0x2D, 0x00, 0x37, 0x00, 0x33, 0x00, 0x42, 0x00, 0x39, 0x00, 0x2D, 0x00, 0x34, 0x00,
0x43, 0x00, 0x41, 0x00, 0x34, 0x00, 0x2D, 0x00, 0x38, 0x00, 0x38, 0x00, 0x46, 0x00, 0x39, 0x00,
0x2D, 0x00, 0x44, 0x00, 0x41, 0x00, 0x32, 0x00, 0x43, 0x00, 0x46, 0x00, 0x33, 0x00, 0x31, 0x00,
0x38, 0x00, 0x45, 0x00, 0x32, 0x00, 0x37, 0x00, 0x35, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00,
];
pub(crate) const FAT_DATA2_OFFSET: usize = 0x9000;
#[rustfmt::skip] // keep this in 16-byte width
pub(crate) const FAT_DATA3: [u8; 0x10] = [
0x0C, 0x00, 0x00, 0x00, 0x28, 0x34, 0x07, 0x9C, 0x7C, 0xBD, 0x65, 0xF9, 0x00, 0x00, 0x00, 0x00,
];
pub(crate) const FAT_DATA3_OFFSET: usize = 0xD000;
pub(crate) const CSW_ADDR: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + CRG_UDC_APP_BUF_LEN;
pub(crate) const CBW_ADDR: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET;
pub(crate) const EP1_IN_BUF: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + 1024;
pub(crate) const EP1_IN_BUF_LEN: usize = 1024;
#[allow(dead_code)]
pub(crate) const EP1_OUT_BUF: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + 2048;
#[allow(dead_code)]
pub(crate) const EP1_OUT_BUF_LEN: usize = 1024;
pub(crate) const MASS_STORAGE_EPADDR_IN: u8 = 0x81;
pub(crate) const MASS_STORAGE_EPADDR_OUT: u8 = 0x01;
pub const FS_MAX_PKT_SIZE: usize = 64;
pub const HS_MAX_PKT_SIZE: usize = 512;
pub(crate) fn enable_mass_storage_eps(this: &mut CorigineUsb, ep_num: u8) {
this.ep_enable(ep_num, USB_RECV, HS_MAX_PKT_SIZE as _, EpType::BulkOutbound);
this.ep_enable(ep_num, USB_SEND, HS_MAX_PKT_SIZE as _, EpType::BulkInbound);
}
// Call from USB_REQ_SET_CONFIGURATION after setting device state to Configured
pub(crate) fn enable_composite_eps(this: &mut CorigineUsb) {
// MSD endpoints
enable_mass_storage_eps(this, 1);
pub(crate) fn enable_serial_eps(this: &mut CorigineUsb) {
// CDC notification IN (interrupt)
this.ep_enable(2, USB_SEND, HS_INT_MPS as _, EpType::IntrInbound);
@ -150,19 +27,14 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
match (value >> 8) as u8 {
USB_DT_DEVICE => {
let mut dd = DeviceDescriptor::composite_with_iad();
let mut dd = DeviceDescriptor::default_usb_acm();
dd.b_max_packet_size0 = 64;
let len = length.min(core::mem::size_of::<DeviceDescriptor>());
ep0_buf[..len].copy_from_slice(&dd.as_ref()[..len]);
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, len, 0);
}
USB_DT_DEVICE_QUALIFIER => {
let mut q = QualifierDescriptor::default_mass_storage();
// For composite the fields still apply. Keep class 0.
q.b_num_configurations = 1;
let len = length.min(core::mem::size_of::<QualifierDescriptor>());
ep0_buf[..len].copy_from_slice(&q.as_ref()[..len]);
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, len, 0);
this.ep_halt(0, USB_RECV);
}
USB_DT_CONFIG => {
let wrote = write_config_hs(ep0_buf);
@ -197,18 +69,7 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, buffsize, 0);
}
USB_DT_BOS => {
let total_length =
core::mem::size_of::<BosDescriptor>() + core::mem::size_of::<ExtCapDescriptor>();
let bos = BosDescriptor::default_mass_storage(total_length as u16, 1);
let ext = ExtCapDescriptor::default_mass_storage((0xfa << 8) | (0x3 << 3));
let response: [&[u8]; 2] = [bos.as_ref(), ext.as_ref()];
let mut idx = 0;
for part in response {
ep0_buf[idx..idx + part.len()].copy_from_slice(part);
idx += part.len();
}
let buffsize = total_length.min(length);
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, buffsize, 0);
this.ep_halt(0, USB_RECV);
}
_ => {
this.ep_halt(0, USB_RECV);
@ -216,90 +77,6 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
}
}
pub fn usb_ep1_bulk_out_complete(
this: &mut CorigineUsb,
buf_addr: usize,
info: u32,
_error: u8,
_residual: u16,
) {
let length = info & 0xFFFF;
let buf = unsafe { core::slice::from_raw_parts(buf_addr as *const u8, info as usize & 0xFFFF) };
let mut cbw = Cbw::default();
cbw.as_mut().copy_from_slice(&buf[..size_of::<Cbw>()]);
let mut csw = Csw::derive();
if UmsState::CommandPhase == this.ms_state && (length == 31) {
// CBW
if cbw.signature == BULK_CBW_SIG {
csw.signature = BULK_CSW_SIG;
csw.tag = cbw.tag;
csw.update_hw();
process_mass_storage_command(this, cbw);
// invalid_cbw = 0;
} else {
crate::println_d!("Invalid CBW, HALT");
this.ep_halt(1, USB_SEND);
this.ep_halt(1, USB_RECV);
// invalid_cbw = 1;
}
} else if UmsState::CommandPhase == this.ms_state && (length != 31) {
crate::println_d!("invalid command");
this.ep_halt(1, USB_SEND);
this.ep_halt(1, USB_RECV);
// invalid_cbw = 1;
} else if UmsState::DataPhase == this.ms_state {
crate::println_d!("data");
//DATA
if let Some((write_offset, len)) = this.callback_wr.take() {
let app_buf = conjure_app_buf();
let disk = conjure_disk();
// update the received data to the disk
disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]);
if let Some((offset, remaining_len)) = this.remaining_wr.take() {
this.setup_big_write(app_buf_addr(), app_buf_len(), offset, remaining_len);
this.ms_state = UmsState::DataPhase;
csw.update_hw();
} else {
csw.residue = 0;
csw.status = 0;
csw.send(this);
}
} else {
crate::println_d!("Data completion reached without destination for data copy! data dropped.");
}
} else {
crate::println_d!("uhhh wtf");
}
}
pub fn usb_ep1_bulk_in_complete(
this: &mut CorigineUsb,
_buf_addr: usize,
info: u32,
_error: u8,
_residual: u16,
) {
// crate::println_d!("bulk IN handler");
let length = info & 0xFFFF;
if UmsState::DataPhase == this.ms_state {
//DATA
if let Some((offset, len)) = this.remaining_rd.take() {
let app_buf = conjure_app_buf();
let disk = conjure_disk();
this.setup_big_read(app_buf, disk, offset, len, None);
this.ms_state = UmsState::DataPhase;
} else {
this.bulk_xfer(1, USB_SEND, CSW_ADDR, 13, 0, 0);
this.ms_state = UmsState::StatusPhase;
}
} else if UmsState::StatusPhase == this.ms_state && length == 13 {
//CSW
this.bulk_xfer(1, USB_RECV, CBW_ADDR, 31, 0, 0);
this.ms_state = UmsState::CommandPhase;
}
}
// ===== CDC Bulk OUT (EP3 OUT) =====
pub fn usb_ep3_bulk_out_complete(
this: &mut CorigineUsb,
@ -412,378 +189,3 @@ pub fn usb_ep2_int_in_complete(
// Typically sends SERIAL_STATE bitmap (carrier detect etc).
// Ignoring - this is a virtual terminal
}
fn process_mass_storage_command(this: &mut CorigineUsb, cbw: Cbw) {
match cbw.cdb[0] {
0x00 => { /* Request the device to report if it is ready */
process_test_unit_ready(this, cbw);
}
0x03 => { /* Transfer status sense data to the host */
process_request_sense(this, cbw);
}
0x12 => { /* Inquity command. Get device information */
process_inquiry_command(this, cbw);
}
0x1E => { /* Prevent or allow the removal of media from a removable
** media device
*/
process_prevent_allow_medium_removal(this, cbw);
}
0x25 => { /* Report current media capacity */
process_report_capacity(this, cbw);
}
0x9e => {
process_read_capacity_16(this, cbw);
}
0x28 => { /* Read (10) Transfer binary data from media to the host */
process_read_command(this, cbw);
}
0x2A => { /* Write (10) Transfer binary data from the host to the
** media
*/
process_write_command(this, cbw);
}
0xAA => { /* Write (12) Transfer binary data from the host to the
** media
*/
process_write12_command(this, cbw);
}
0x01 | /* Position a head of the drive to zero track */
0x04 | /* Format unformatted media */
0x1A |
0x1B | /* Request a request a removable-media device to load or
** unload its media
*/
0x1D | /* Perform a hard reset and execute diagnostics */
0x23 | /* Read Format Capacities. Report current media capacity and
** formattable capacities supported by media
*/
0x2B | /* Seek the device to a specified address */
0x2E | /* Transfer binary data from the host to the media and
** verify data
*/
0x2F | /* Verify data on the media */
0x55 | /* Allow the host to set parameters in a peripheral */
0x5A | /* Report parameters to the host */
0xA8 /* Read (12) Transfer binary data from the media to the host */ => {
process_unsupported_command(this, cbw);
}
_ => {
process_unsupported_command(this, cbw);
}
}
}
fn process_test_unit_ready(this: &mut CorigineUsb, _cbw: Cbw) {
let mut csw = Csw::derive();
csw.residue = 0;
csw.status = 0;
csw.send(this);
}
fn process_request_sense(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
if cbw.data_transfer_length == 0 {
csw.residue = 0;
csw.status = 0;
csw.send(this);
// crate::println_d!("UMS_STATE_STATUS_PHASE\r\n");
} else if cbw.flags & 0x80 != 0 {
if cbw.data_transfer_length < 18 {
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..18].fill(0);
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, cbw.data_transfer_length as usize, 0, 0);
this.ms_state = UmsState::DataPhase;
csw.residue = 0;
csw.status = 0;
} else if cbw.data_transfer_length >= 18 {
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..18].fill(0);
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 18, 0, 0);
this.ms_state = UmsState::DataPhase;
csw.residue = cbw.data_transfer_length - 18;
csw.status = 0;
}
}
csw.update_hw();
}
fn process_inquiry_command(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
let inquiry_data = InquiryResponse {
peripheral_device_type: 0,
rmb: 0,
version: 0,
response_data_format: 1,
additional_length: 31,
reserved1: 0,
reserved2: 0,
reserved3: 0,
vendor_identification: *b"Bao Semi",
product_identification: *b"USB update vdisk",
product_revision_level: *b"demo",
};
if cbw.data_transfer_length == 0 {
csw.residue = 0;
csw.status = 0;
csw.send(this);
} else if cbw.flags & 0x80 != 0 {
if cbw.data_transfer_length < 36 {
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..36].fill(0);
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, cbw.data_transfer_length as usize, 0, 0);
this.ms_state = UmsState::DataPhase;
csw.residue = 0;
csw.status = 0;
} else if cbw.data_transfer_length as usize >= size_of::<InquiryResponse>() {
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..size_of::<InquiryResponse>()].copy_from_slice(inquiry_data.as_ref());
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, size_of::<InquiryResponse>(), 0, 0);
this.ms_state = UmsState::DataPhase;
csw.residue = cbw.data_transfer_length - size_of::<InquiryResponse>() as u32;
csw.status = 0;
}
}
csw.update_hw();
}
fn process_prevent_allow_medium_removal(this: &mut CorigineUsb, _cbw: Cbw) {
let mut csw = Csw::derive();
csw.residue = 0;
csw.status = 0;
csw.send(this);
}
fn process_report_capacity(this: &mut CorigineUsb, cbw: Cbw) {
crate::println_d!("REPORT CAPACITY");
let mut csw = Csw::derive();
let rc_lba = RAMDISK_LEN / SECTOR_SIZE as usize - 1;
let rc_bl: u32 = SECTOR_SIZE as u32;
let mut capacity = [0u8; 8];
capacity[..4].copy_from_slice(&rc_lba.to_be_bytes());
capacity[4..].copy_from_slice(&rc_bl.to_be_bytes());
if cbw.flags & 0x80 != 0 {
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..8].fill(0);
ep1_in[..8].copy_from_slice(&capacity);
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 8, 0, 0);
this.ms_state = UmsState::DataPhase;
} else {
csw.send(this);
}
csw.residue = 0;
csw.status = 0;
csw.update_hw();
}
fn process_read_capacity_16(this: &mut CorigineUsb, _cbw: Cbw) {
let rc_lba = (RAMDISK_LEN / SECTOR_SIZE as usize - 1) as u64;
let rc_bl: u32 = SECTOR_SIZE as u32;
let mut response = [0u8; 32];
response[..8].copy_from_slice(&rc_lba.to_be_bytes());
response[8..12].copy_from_slice(&rc_bl.to_be_bytes());
let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) };
ep1_in[..32].copy_from_slice(&response);
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 32, 0, 0);
this.ms_state = UmsState::DataPhase;
}
fn process_unsupported_command(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
csw.residue = 0;
csw.status = 0;
if cbw.flags & 0x80 != 0 {
this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 0, 0, 0);
this.ms_state = UmsState::DataPhase;
} else {
csw.send(this);
}
csw.update_hw();
}
fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
let mut lba;
let mut length;
lba = (cbw.cdb[4] as u32) << 8;
lba |= cbw.cdb[5] as u32;
length = (cbw.cdb[7] as u32) << 8;
length |= cbw.cdb[8] as u32;
length *= SECTOR_SIZE as u32;
if cbw.flags & 0x80 == 0 {
csw.residue = cbw.data_transfer_length;
csw.status = 2;
this.setup_big_write(
app_buf_addr(),
app_buf_len(),
lba as usize * SECTOR_SIZE as usize,
length as usize,
);
this.ms_state = UmsState::DataPhase;
return;
}
crate::println_d!(
"DISK READ address = 0x{:x}, length = 0x{:x}",
RAMDISK_ADDRESS + lba as usize * SECTOR_SIZE as usize,
length
);
if cbw.data_transfer_length == 0 {
csw.residue = 0;
csw.status = 2;
csw.send(this);
} else {
csw.residue = 0;
csw.status = 0;
if cbw.data_transfer_length < length {
length = cbw.data_transfer_length;
csw.residue = cbw.data_transfer_length;
csw.status = 1;
} else if cbw.data_transfer_length > length {
csw.residue = cbw.data_transfer_length - length;
csw.status = 1;
}
let app_buf = conjure_app_buf();
let disk = conjure_disk();
this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None);
this.ms_state = UmsState::DataPhase;
}
}
fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
let mut lba: u32;
let mut length: u32;
lba = (cbw.cdb[4] as u32) << 8;
lba |= cbw.cdb[5] as u32;
length = (cbw.cdb[7] as u32) << 8;
length |= cbw.cdb[8] as u32;
length *= SECTOR_SIZE as u32;
if cbw.flags & 0x80 != 0 {
csw.residue = cbw.data_transfer_length;
csw.status = 2;
let app_buf = conjure_app_buf();
let disk = conjure_disk();
this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None);
this.ms_state = UmsState::DataPhase;
csw.update_hw();
return;
}
crate::println_d!(
"Write address = 0x{:x}, length = 0x{:x}",
RAMDISK_ADDRESS + lba as usize * SECTOR_SIZE as usize,
length
);
if cbw.data_transfer_length == 0 {
csw.residue = 0;
csw.status = 2;
csw.send(this);
} else {
csw.residue = 0;
csw.status = 0;
if cbw.data_transfer_length > length {
csw.residue = cbw.data_transfer_length - length;
length = cbw.data_transfer_length;
csw.status = 1;
} else if cbw.data_transfer_length < length {
csw.residue = cbw.data_transfer_length;
csw.status = 1;
length = cbw.data_transfer_length;
}
this.setup_big_write(
app_buf_addr(),
app_buf_len(),
lba as usize * SECTOR_SIZE as usize,
length as usize,
);
this.ms_state = UmsState::DataPhase;
}
csw.update_hw();
}
fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) {
let mut csw = Csw::derive();
let mut lba;
let mut length;
lba = (cbw.cdb[2] as u32) << 24;
lba |= (cbw.cdb[3] as u32) << 16;
lba |= (cbw.cdb[4] as u32) << 8;
lba |= (cbw.cdb[5] as u32) << 0;
length = (cbw.cdb[6] as u32) << 24;
length |= (cbw.cdb[7] as u32) << 16;
length |= (cbw.cdb[8] as u32) << 8;
length |= (cbw.cdb[9] as u32) << 0;
length *= SECTOR_SIZE as u32;
crate::println_d!("write12 of {} bytes", length);
if cbw.flags & 0x80 != 0 {
csw.residue = cbw.data_transfer_length;
csw.status = 2;
// note: zero-length transfer but we still update the buffer because that's what the reference driver
// does
let app_buf = conjure_app_buf();
let disk = conjure_disk();
this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize, None);
this.ms_state = UmsState::DataPhase;
csw.update_hw();
return;
}
if cbw.data_transfer_length == 0 {
csw.residue = 0;
csw.status = 2;
csw.send(this);
return;
} else {
csw.residue = 0;
csw.status = 0;
if cbw.data_transfer_length > length {
csw.residue = cbw.data_transfer_length - length;
length = cbw.data_transfer_length;
} else if cbw.data_transfer_length < length {
csw.residue = cbw.data_transfer_length;
csw.status = 2;
length = cbw.data_transfer_length;
}
this.setup_big_write(
app_buf_addr(),
app_buf_len(),
lba as usize * SECTOR_SIZE as usize,
length as usize,
);
this.ms_state = UmsState::DataPhase;
}
csw.update_hw();
}
// the 1024 is reserved for the CSW/CBW records
pub(crate) fn app_buf_addr() -> usize { CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + 1024 }
// the length subtracts the reserved 1024, and adds one page as a hack for testing - TODO: fix that
pub(crate) fn app_buf_len() -> usize { CRG_UDC_APP_BUFSIZE - 1024 + 4096 }
pub(crate) fn conjure_app_buf() -> &'static mut [u8] {
unsafe { core::slice::from_raw_parts_mut(app_buf_addr() as *mut u8, app_buf_len()) } // added +1 page in CRG_UDC_MEMBASE
}
pub(crate) fn conjure_disk() -> &'static mut [u8] {
unsafe { core::slice::from_raw_parts_mut(RAMDISK_ADDRESS as *mut u8, RAMDISK_LEN) }
}

View file

@ -74,8 +74,6 @@ const USB_DT_SS_ENDPOINT_COMP: u8 = 0x30;
#[allow(dead_code)]
const USB_DT_SSP_ISOC_ENDPOINT_COMP: u8 = 0x31;
const USB_CAP_TYPE_EXT: u8 = 0x2;
// ===== Constants for CDC/IAD and endpoints =====
const USB_DT_CS_INTERFACE: u8 = 0x24;
#[allow(dead_code)]
@ -139,55 +137,14 @@ const PRODUCT: &'static str = "Dabao";
const SERIAL: &'static str = "TODO";
impl DeviceDescriptor {
#[allow(dead_code)]
pub fn default_mass_storage() -> Self {
pub fn default_usb_acm() -> Self {
Self {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_DEVICE,
b_cd_usb: 0x0200,
b_device_class: 0,
b_device_sub_class: 0,
b_device_protocol: 0,
b_max_packet_size0: 0x40,
id_vendor: VENDOR_ID,
id_product: PRODUCT_ID,
b_cd_device: 0x0101,
i_manufacturer: 0x01, // string index for manufacturer
i_product: 0x02, // string index for product
i_serial_number: 0x03, // string index for serial number
b_num_configurations: 1,
}
}
#[allow(dead_code)]
pub fn default_composite() -> Self {
Self {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_DEVICE,
b_cd_usb: 0x0200,
b_device_class: 0, // composite via per-interface classing
b_device_sub_class: 0,
b_device_protocol: 0,
b_max_packet_size0: 0x40,
id_vendor: VENDOR_ID,
id_product: PRODUCT_ID,
b_cd_device: 0x0101,
i_manufacturer: 0x01,
i_product: 0x02,
i_serial_number: 0x03,
b_num_configurations: 1,
}
}
// Windows-friendly composite: EF/02/01 indicates IAD-present composite
pub fn composite_with_iad() -> Self {
Self {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_DEVICE,
b_cd_usb: 0x0200,
b_device_class: 0xEF, // Miscellaneous
b_device_sub_class: 0x02, // Common Class
b_device_protocol: 0x01, // Interface Association Descriptor (IAD)
b_device_class: 0x00, // Per-interface classification
b_device_sub_class: 0x00,
b_device_protocol: 0x00,
b_max_packet_size0: 0x40,
id_vendor: VENDOR_ID,
id_product: PRODUCT_ID,
@ -211,46 +168,6 @@ impl AsRef<[u8]> for DeviceDescriptor {
}
}
/* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */
#[allow(dead_code)]
#[repr(C, packed)]
struct QualifierDescriptor {
pub b_length: u8,
pub b_descriptor_type: u8,
pub b_cd_usb: u16,
pub b_device_class: u8,
pub b_device_sub_class: u8,
pub b_device_protocol: u8,
pub b_max_packet_size0: u8,
pub b_num_configurations: u8,
pub b_reserved: u8,
}
impl QualifierDescriptor {
pub fn default_mass_storage() -> Self {
Self {
b_length: 0xA,
b_descriptor_type: 0x6,
b_cd_usb: 0x200,
b_device_class: 0x0,
b_device_sub_class: 0x0,
b_device_protocol: 0x0,
b_max_packet_size0: 0x40,
b_num_configurations: 0x1,
b_reserved: 0x0,
}
}
}
impl AsRef<[u8]> for QualifierDescriptor {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(
self as *const QualifierDescriptor as *const u8,
core::mem::size_of::<QualifierDescriptor>(),
) as &[u8]
}
}
}
#[allow(dead_code)]
#[repr(C, packed)]
struct ConfigDescriptor {
@ -263,21 +180,6 @@ struct ConfigDescriptor {
pub bm_attributes: u8,
pub b_max_power: u8,
}
impl ConfigDescriptor {
#[allow(dead_code)]
pub fn default_mass_storage(total_length: u16) -> Self {
ConfigDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_CONFIG,
w_total_length: total_length,
b_num_interfaces: 1,
b_configuration_value: 1,
i_configuration: 0x0,
bm_attributes: 0xC0,
b_max_power: 250,
}
}
}
impl AsRef<[u8]> for ConfigDescriptor {
fn as_ref(&self) -> &[u8] {
unsafe {
@ -303,26 +205,6 @@ struct InterfaceDescriptor {
pub i_interface: u8,
}
impl InterfaceDescriptor {
pub fn default_mass_storage() -> Self {
InterfaceDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_INTERFACE,
b_interface_number: 0,
b_alternate_setting: 0,
b_num_endpoints: 2,
b_interface_class: 0x08, // mass storage class
b_interface_sub_class: 0x06,
b_interface_protocol: 0x50,
i_interface: 0x0,
}
}
pub fn mass_storage(if_num: u8) -> Self {
let mut d = InterfaceDescriptor::default_mass_storage();
d.b_interface_number = if_num;
d
}
pub fn cdc_comm(if_num: u8) -> Self {
Self {
b_length: core::mem::size_of::<Self>() as u8,
@ -372,17 +254,6 @@ struct EndpointDescriptor {
b_interval: u8,
}
impl EndpointDescriptor {
pub fn default_mass_storage(addr: u8, max_packet_size: u16) -> Self {
EndpointDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_ENDPOINT,
b_endpoint_address: addr,
b_m_attributes: 0x02,
w_max_packet_size: max_packet_size,
b_interval: 0x0,
}
}
fn cdc_notif_ep_hs() -> Self {
EndpointDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
@ -481,179 +352,6 @@ impl AsMut<[u8]> for CtrlRequest {
}
}
#[allow(dead_code)]
#[repr(C, packed)]
struct BosDescriptor {
pub b_length: u8,
pub b_descriptor_type: u8,
pub w_total_length: u16,
pub b_num_device_caps: u8,
}
impl BosDescriptor {
pub fn default_mass_storage(total_length: u16, num_caps: u8) -> Self {
BosDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_BOS,
w_total_length: total_length,
b_num_device_caps: num_caps,
}
}
}
impl AsRef<[u8]> for BosDescriptor {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(
self as *const BosDescriptor as *const u8,
core::mem::size_of::<BosDescriptor>(),
) as &[u8]
}
}
}
#[allow(dead_code)]
#[repr(C, packed)]
struct ExtCapDescriptor {
pub b_length: u8,
pub b_descriptor_type: u8,
pub b_dev_capability_type: u8,
pub b_mattributes: u32,
}
impl ExtCapDescriptor {
pub fn default_mass_storage(attributes: u32) -> Self {
ExtCapDescriptor {
b_length: core::mem::size_of::<Self>() as u8,
b_descriptor_type: USB_DT_DEVICE_CAPABILITY,
b_dev_capability_type: USB_CAP_TYPE_EXT,
b_mattributes: attributes,
}
}
}
impl AsRef<[u8]> for ExtCapDescriptor {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(
self as *const ExtCapDescriptor as *const u8,
core::mem::size_of::<ExtCapDescriptor>(),
) as &[u8]
}
}
}
#[allow(dead_code)]
pub(crate) const USB_LPM_SUPPORT: u8 = 1 << 1; /* supports LPM */
#[allow(dead_code)]
pub(crate) const USB_BESL_SUPPORT: u8 = 1 << 2; /* supports BESL */
#[allow(dead_code)]
pub(crate) const USB_BESL_BASELINE_VALID: u8 = 1 << 3; /* Baseline BESL valid*/
#[allow(dead_code)]
pub(crate) const USB_BESL_DEEP_VALID: u8 = 1 << 4; /* Deep BESL valid */
#[repr(C, packed)]
#[derive(Default, Debug)]
struct Cbw {
signature: u32, // Contains 'USBC'
tag: u32, // Unique per command id
data_transfer_length: u32, // Size of the data
flags: u8, // Direction in bit 7
lun: u8, // LUN (normally 0)
length: u8, // Of the CDB, <= MAX_COMMAND_SIZE
cdb: [u8; 16], // Command Data Block
}
impl AsMut<[u8]> for Cbw {
fn as_mut(&mut self) -> &mut [u8] {
unsafe {
core::slice::from_raw_parts_mut(self as *mut Cbw as *mut u8, core::mem::size_of::<Cbw>())
as &mut [u8]
}
}
}
impl AsRef<[u8]> for Cbw {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(self as *const Cbw as *const u8, core::mem::size_of::<Cbw>())
as &[u8]
}
}
}
const BULK_CBW_SIG: u32 = 0x43425355; /* Spells out USBC */
#[repr(C, packed)]
#[derive(Default)]
struct Csw {
pub signature: u32, // Should = 'USBS'
pub tag: u32, // Same as original command
pub residue: u32, // Amount not transferred
pub status: u8, // See below
}
impl Csw {
fn derive() -> Csw {
let mut csw = Csw::default();
csw.as_mut().copy_from_slice(unsafe {
core::slice::from_raw_parts(handlers::CSW_ADDR as *mut u8, size_of::<Csw>())
});
csw
}
fn update_hw(&self) {
let csw_buf = unsafe { core::slice::from_raw_parts_mut(CSW_ADDR as *mut u8, size_of::<Csw>()) };
csw_buf.copy_from_slice(self.as_ref());
}
fn send(&self, usb: &mut bao1x_hal::usb::driver::CorigineUsb) {
let csw_buf = unsafe { core::slice::from_raw_parts_mut(CSW_ADDR as *mut u8, size_of::<Csw>()) };
csw_buf.copy_from_slice(self.as_ref());
usb.bulk_xfer(1, bao1x_hal::usb::driver::USB_SEND, CSW_ADDR, size_of::<Csw>(), 0, 0);
usb.ms_state = bao1x_hal::usb::driver::UmsState::StatusPhase;
}
}
const BULK_CSW_SIG: u32 = 0x53425355; /* Spells out 'USBS' */
impl AsRef<[u8]> for Csw {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(self as *const Csw as *const u8, core::mem::size_of::<Csw>())
as &[u8]
}
}
}
impl AsMut<[u8]> for Csw {
fn as_mut(&mut self) -> &mut [u8] {
unsafe {
core::slice::from_raw_parts_mut(self as *mut Csw as *mut u8, core::mem::size_of::<Csw>())
as &mut [u8]
}
}
}
#[repr(C, packed)]
struct InquiryResponse {
pub peripheral_device_type: u8, // Byte 0: Peripheral Device Type (PDT)
pub rmb: u8, // Byte 1: Removable Media Bit (RMB) and Device Type Modifier
pub version: u8, // Byte 2: ISO/ECMA/ANSI Version
pub response_data_format: u8, // Byte 3: Response Data Format (RDF) and capabilities
pub additional_length: u8, // Byte 4: Additional Length (number of bytes after byte 7)
pub reserved1: u8, // Byte 5: Reserved
pub reserved2: u8, // Byte 6: Reserved
pub reserved3: u8, // Byte 7: Reserved
pub vendor_identification: [u8; 8], // Byte 8-15: Vendor Identification (ASCII)
pub product_identification: [u8; 16], // Byte 16-31: Product Identification (ASCII)
pub product_revision_level: [u8; 4], // Byte 32-35: Product Revision Level (ASCII)
}
impl AsRef<[u8]> for InquiryResponse {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(
self as *const InquiryResponse as *const u8,
core::mem::size_of::<InquiryResponse>(),
) as &[u8]
}
}
}
// ===== IAD descriptor to group the CDC function =====
#[repr(C, packed)]
struct IadDescriptor {
@ -809,16 +507,6 @@ impl AsRef<[u8]> for CdcUnionFuncDesc {
}
// ===== Composite Config descriptors (HS and FS) =====
// Layout:
// Config
// IAD (CDC group)
// IF0 CDC Comm
// CDC Header, CallMgmt, ACM, Union
// EP interrupt IN
// IF1 CDC Data
// EP bulk OUT, EP bulk IN
// IF2 MSD
// EP bulk IN, EP bulk OUT
fn config_total_len_hs() -> usize {
core::mem::size_of::<ConfigDescriptor>()
@ -830,9 +518,7 @@ fn config_total_len_hs() -> usize {
+ core::mem::size_of::<CdcUnionFuncDesc>()
+ core::mem::size_of::<EndpointDescriptor>() // CDC notif
+ core::mem::size_of::<InterfaceDescriptor>() // CDC Data
+ core::mem::size_of::<EndpointDescriptor>() * 2 // CDC data bulk
+ core::mem::size_of::<InterfaceDescriptor>() // MSD
+ core::mem::size_of::<EndpointDescriptor>() * 2 // MSD bulk
+ core::mem::size_of::<EndpointDescriptor>() * 2 // CDC data bulk
}
fn config_total_len_fs() -> usize {
@ -844,7 +530,6 @@ fn config_total_len_fs() -> usize {
fn write_config_hs(ep0_buf: &mut [u8]) -> usize {
let if_cdc_comm: u8 = 0;
let if_cdc_data: u8 = 1;
let if_msd: u8 = 2;
let total_len = config_total_len_hs() as u16;
@ -852,7 +537,7 @@ fn write_config_hs(ep0_buf: &mut [u8]) -> usize {
b_length: core::mem::size_of::<ConfigDescriptor>() as u8,
b_descriptor_type: USB_DT_CONFIG,
w_total_length: total_len,
b_num_interfaces: 3,
b_num_interfaces: 2,
b_configuration_value: 1,
i_configuration: 0,
bm_attributes: 0xC0, // self powered, no remote wakeup
@ -871,11 +556,7 @@ fn write_config_hs(ep0_buf: &mut [u8]) -> usize {
let ep_data_out = EndpointDescriptor::cdc_data_out_hs();
let ep_data_in = EndpointDescriptor::cdc_data_in_hs();
let if_msd_desc = InterfaceDescriptor::mass_storage(if_msd);
let ep_msd_in = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_IN, HS_MAX_PKT_SIZE as _);
let ep_msd_out = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_OUT, HS_MAX_PKT_SIZE as _);
let parts: [&[u8]; 14] = [
let parts: [&[u8]; 11] = [
config.as_ref(),
iad.as_ref(),
if_comm.as_ref(),
@ -887,9 +568,6 @@ fn write_config_hs(ep0_buf: &mut [u8]) -> usize {
if_data.as_ref(),
ep_data_out.as_ref(),
ep_data_in.as_ref(),
if_msd_desc.as_ref(),
ep_msd_in.as_ref(),
ep_msd_out.as_ref(),
];
let mut idx = 0;
@ -900,11 +578,10 @@ fn write_config_hs(ep0_buf: &mut [u8]) -> usize {
idx
}
// Writes FS other-speed config into ep0_buf and returns the byte count
// Writes FS config into ep0_buf and returns the byte count
fn write_config_fs(ep0_buf: &mut [u8]) -> usize {
let if_cdc_comm: u8 = 0;
let if_cdc_data: u8 = 1;
let if_msd: u8 = 2;
let total_len = config_total_len_fs() as u16;
@ -912,7 +589,7 @@ fn write_config_fs(ep0_buf: &mut [u8]) -> usize {
b_length: core::mem::size_of::<ConfigDescriptor>() as u8,
b_descriptor_type: USB_DT_CONFIG,
w_total_length: total_len,
b_num_interfaces: 3,
b_num_interfaces: 2,
b_configuration_value: 1,
i_configuration: 0,
bm_attributes: 0xC0,
@ -931,11 +608,8 @@ fn write_config_fs(ep0_buf: &mut [u8]) -> usize {
let ep_data_out = EndpointDescriptor::cdc_data_out_fs();
let ep_data_in = EndpointDescriptor::cdc_data_in_fs();
let if_msd_desc = InterfaceDescriptor::mass_storage(if_msd);
let ep_msd_in = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_IN, FS_MAX_PKT_SIZE as _);
let ep_msd_out = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_OUT, FS_MAX_PKT_SIZE as _);
let parts: [&[u8]; 14] = [
let parts: [&[u8]; 11] = [
// Changed from 14 to 11
config.as_ref(),
iad.as_ref(),
if_comm.as_ref(),
@ -947,9 +621,6 @@ fn write_config_fs(ep0_buf: &mut [u8]) -> usize {
if_data.as_ref(),
ep_data_out.as_ref(),
ep_data_in.as_ref(),
if_msd_desc.as_ref(),
ep_msd_in.as_ref(),
ep_msd_out.as_ref(),
];
let mut idx = 0;