writes now working, barely.

This commit is contained in:
bunnie 2023-01-10 05:21:11 +08:00
parent 5c1518cee6
commit 3355f3b5f4
6 changed files with 206 additions and 42 deletions

View file

@ -47,5 +47,6 @@ rand_chacha = "0.3.1"
precursor = ["utralib/precursor"] precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"] hosted = ["utralib/hosted"]
renode = ["utralib/renode"] renode = ["utralib/renode"]
chain = []
mjolnir = [] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care. mjolnir = [] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care.
default = [] default = ["chain"]

View file

@ -8,6 +8,7 @@ use std::collections::BTreeMap;
use std::convert::TryInto; use std::convert::TryInto;
const WRITE_TIMEOUT_MS: u64 = 10_000; const WRITE_TIMEOUT_MS: u64 = 10_000;
const READ_TIMEOUT_MS: u64 = 10_000;
fn handle_usb(_irq_no: usize, arg: *mut usize) { fn handle_usb(_irq_no: usize, arg: *mut usize) {
let usb = unsafe { &mut *(arg as *mut SpinalUsbDevice) }; let usb = unsafe { &mut *(arg as *mut SpinalUsbDevice) };
@ -200,8 +201,10 @@ pub struct SpinalUsbDevice {
address: AtomicUsize, address: AtomicUsize,
// bit vector to track if a read is allowed. This prevents a race condition between polled reads and interrupted reads. // bit vector to track if a read is allowed. This prevents a race condition between polled reads and interrupted reads.
read_allowed: AtomicU16, read_allowed: AtomicU16,
// last descriptor in chain. Used to check if a transaction is in progress. // last descriptor in write (IN packet; device->host) chain. Used to check if a transaction is in progress.
last_descriptor: Arc::<Mutex<[Option<SpinalUdcDescriptor>; 16]>>, last_wr_desc: Arc::<Mutex<[Option<SpinalUdcDescriptor>; 16]>>,
// last descriptor in read (OUT packet; host->device) chain. Used to check if a transaction is in progress.
last_rd_desc: Arc::<Mutex<[Option<SpinalUdcDescriptor>; 16]>>,
} }
impl SpinalUsbDevice { impl SpinalUsbDevice {
pub fn new(sid: xous::SID) -> SpinalUsbDevice { pub fn new(sid: xous::SID) -> SpinalUsbDevice {
@ -237,7 +240,8 @@ impl SpinalUsbDevice {
tt: ticktimer_server::Ticktimer::new().unwrap(), tt: ticktimer_server::Ticktimer::new().unwrap(),
address: AtomicUsize::new(0), address: AtomicUsize::new(0),
read_allowed: AtomicU16::new(0), read_allowed: AtomicU16::new(0),
last_descriptor: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])), last_wr_desc: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])),
last_rd_desc: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])),
}; };
xous::claim_interrupt( xous::claim_interrupt(
@ -272,7 +276,8 @@ impl SpinalUsbDevice {
tt: ticktimer_server::Ticktimer::new().unwrap(), tt: ticktimer_server::Ticktimer::new().unwrap(),
address: AtomicUsize::new(0), address: AtomicUsize::new(0),
read_allowed: AtomicU16::new(0), read_allowed: AtomicU16::new(0),
last_descriptor: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])), last_wr_desc: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])),
last_rd_desc: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])),
} }
} }
pub fn get_iface(&self) -> SpinalUsbMgmt { pub fn get_iface(&self) -> SpinalUsbMgmt {
@ -440,6 +445,73 @@ impl SpinalUsbDevice {
); );
setup setup
} }
/*
pub(crate) fn read_blocking_bulk(&self, ep_addr: EndpointAddress, buf: &mut [u8]) -> Result<usize> {
// in the case that a transfer longer than a packet is expected, this call blocks until
// we have read in as much data as we can hold given our descriptor chain depth.
assert!(ep_addr.index() != 0); // long buffers are not allowed on EP0
let mut ep_status = self.status_read_volatile(ep_addr.index());
let mut descriptor = self.descriptor_from_head_and_chain(head_offset, 0, packet_len);
for (chain_offset, sub_buf) in buf.chunks_mut(packet_len).enumerate() {
descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_desc_flags(UsbDirection::Out,
false, true, false);
for b in sub_buf.iter_mut() {
*b = 0; // zeroize the buffer between transactions, to avoid data leakage in unused areas
}
if chain_offset == (max_chain - 1) || sub_buf.len() != packet_len {
// on the very last packet (either by chain exhaustion, or buffer source exhaustion), set completion_on_full interrupt
descriptor.set_next_desc_and_len(0, sub_buf.len());
} else {
// if not the last packet, setup the linked list pointer
let next_addr = self.next_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_next_desc_and_len(next_addr, sub_buf.len());
}
descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress
log::info!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor);
}
ep_status.set_max_packet_size(packet_len as _);
// this is reset to 0 after every transaction by the hardware, so we must reset it
ep_status.set_head_offset(head_offset as u32);
// this is required to commit the ep_status record once all the setup is done
self.status_write_volatile(ep_addr.index(), ep_status);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
// zeroize the return buffer now -- gives us something to do while the data comes in!
for d in buf.iter_mut() {
*d = 0;
}
// now busy-wait for the packet to come in!
let now = self.tt.elapsed_ms();
let mut last_secs = 5;
while descriptor.in_progress() && (self.tt.elapsed_ms() - now < READ_TIMEOUT_MS) {
xous::yield_slice();
let secs = (self.tt.elapsed_ms() - now) / 1_0000;
if last_secs != secs {
log::info!("waiting for rd linked list to complete...");
last_secs = secs;
}
}
// now copy results to buf
for (chain_offset, sub_buf) in buf.chunks_mut(packet_len).enumerate() {
let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
// buffer was zeroized as we were waiting for data to come in, so we can safely do partial copies of data here
for (desc_offset, word_chunk) in sub_buf.chunks_mut(size_of::<u32>()).enumerate() {
word_chunk.copy_from_slice(
&descriptor.read_data(desc_offset)
.to_le_bytes()[..word_chunk.len()]) // the upper limit index ensures that if sub_buf is an odd number of bytes, we don't panic
}
}
log::info!("bulk read {} bytes: {:x?}", buf.len(), buf);
Ok(buf.len())
}
*/
} }
const CHAINED_BUF_LEN: usize = 512; const CHAINED_BUF_LEN: usize = 512;
@ -677,14 +749,16 @@ impl UsbBus for SpinalUsbDevice {
if buf.len() > max_chain * packet_len { if buf.len() > max_chain * packet_len {
Err(UsbError::BufferOverflow) Err(UsbError::BufferOverflow)
} else if buf.len() <= packet_len { // no-chain path } else if buf.len() <= packet_len { // no-chain path
// TODO: merge this with the path below, mostly to reduce code space. This was kept "intact" so enumeration
// would work while we figured out the details of chaining on bulk transfers.
#[cfg(feature="mjolnir")] // mjolnir is so powerful, one must halt the USB core entirely for it to be weilded #[cfg(feature="mjolnir")] // mjolnir is so powerful, one must halt the USB core entirely for it to be weilded
if ep_addr.index() == 1 { self.udc_hard_halt(ep_addr.index()); } if ep_addr.index() == 1 { self.udc_hard_halt(ep_addr.index()); }
let mut ep_status = self.status_read_volatile(ep_addr.index()); let mut ep_status = self.status_read_volatile(ep_addr.index());
if ep_addr.index() != 0 { if ep_addr.index() != 0 {
if let Some(final_descriptor) = &self.last_descriptor.lock().unwrap()[ep_addr.index()] { if let Some(final_descriptor) = &self.last_wr_desc.lock().unwrap()[ep_addr.index()] {
let now = self.tt.elapsed_ms(); let now = self.tt.elapsed_ms();
log::info!("checking final descriptor {:x?}", final_descriptor); log::debug!("checking final descriptor {:x?}", final_descriptor);
let mut last_print = 10; let mut last_print = 10;
while (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) while (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS)
&& final_descriptor.in_progress() { && final_descriptor.in_progress() {
@ -732,7 +806,7 @@ impl UsbBus for SpinalUsbDevice {
false, true, false); false, true, false);
} }
descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress
self.last_descriptor.lock().unwrap()[ep_addr.index()] = Some(descriptor); self.last_wr_desc.lock().unwrap()[ep_addr.index()] = Some(descriptor);
//if ep_addr.index() != 0 { //if ep_addr.index() != 0 {
// log::info!("WR PREdesc{}: {:?}", ep_addr.index(), descriptor); // log::info!("WR PREdesc{}: {:?}", ep_addr.index(), descriptor);
//} //}
@ -760,15 +834,16 @@ impl UsbBus for SpinalUsbDevice {
//} //}
Ok(buf.len()) Ok(buf.len())
} else { } else {
if let Some(final_descriptor) = &self.last_descriptor.lock().unwrap()[ep_addr.index()] { assert!(ep_addr.index() != 0); // long buffers are not allowed on EP0
log::info!("ll checking final descriptor {:x?}", final_descriptor); if let Some(final_descriptor) = &self.last_wr_desc.lock().unwrap()[ep_addr.index()] {
log::debug!("llwr checking final descriptor {:x?}", final_descriptor);
let now = self.tt.elapsed_ms(); let now = self.tt.elapsed_ms();
let mut last_secs = 5; let mut last_secs = 5;
while final_descriptor.in_progress() && (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) { while final_descriptor.in_progress() && (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) {
xous::yield_slice(); xous::yield_slice();
let secs = (self.tt.elapsed_ms() - now) / 1_0000; let secs = (self.tt.elapsed_ms() - now) / 1_0000;
if last_secs != secs { if last_secs != secs {
log::info!("waiting for linked list to complete..."); log::debug!("waiting for wr linked list to complete...");
last_secs = secs; last_secs = secs;
} }
} }
@ -794,16 +869,14 @@ impl UsbBus for SpinalUsbDevice {
if chain_offset == (max_chain - 1) || sub_buf.len() != packet_len { if chain_offset == (max_chain - 1) || sub_buf.len() != packet_len {
// on the very last packet (either by chain exhaustion, or buffer source exhaustion), set completion_on_full interrupt // on the very last packet (either by chain exhaustion, or buffer source exhaustion), set completion_on_full interrupt
descriptor.set_next_desc_and_len(0, sub_buf.len()); descriptor.set_next_desc_and_len(0, sub_buf.len());
descriptor.set_desc_flags(UsbDirection::In,
false, true, false);
} else { } else {
// if not the last packet, setup the linked list pointer // if not the last packet, setup the linked list pointer
let next_addr = self.next_from_head_and_chain(head_offset, chain_offset, packet_len); let next_addr = self.next_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_next_desc_and_len(next_addr, sub_buf.len()); descriptor.set_next_desc_and_len(next_addr, sub_buf.len());
} }
descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress
log::info!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor); log::debug!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor);
self.last_descriptor.lock().unwrap()[ep_addr.index()] = Some(descriptor); self.last_wr_desc.lock().unwrap()[ep_addr.index()] = Some(descriptor);
} }
ep_status.set_max_packet_size(packet_len as _); ep_status.set_max_packet_size(packet_len as _);
@ -896,7 +969,13 @@ impl UsbBus for SpinalUsbDevice {
// we do get spurious reads on EP0 transactions, so this code prevents these from proceeding // we do get spurious reads on EP0 transactions, so this code prevents these from proceeding
// this is because the EP0 interrupt will fire off a poll event that also causes a read of all the endpoints. // this is because the EP0 interrupt will fire off a poll event that also causes a read of all the endpoints.
// the interrupt hasn't triggered, don't allow the read // the interrupt hasn't triggered, don't allow the read
return Err(UsbError::WouldBlock); let ep_status = self.status_read_volatile(ep_addr.index());
let descriptor = self.descriptor_from_status(&ep_status);
if descriptor.offset() == 0 {
return Err(UsbError::WouldBlock);
} else {
log::info!("bypassing WouldBlock");
}
} else { } else {
// this load-store race condition is OK because the interrupt handler doesn't modify this // this load-store race condition is OK because the interrupt handler doesn't modify this
let masked = self.read_allowed.load(Ordering::Relaxed) & !(1 << ep_addr.index() as u16); // clear the read mask let masked = self.read_allowed.load(Ordering::Relaxed) & !(1 << ep_addr.index() as u16); // clear the read mask
@ -915,6 +994,29 @@ impl UsbBus for SpinalUsbDevice {
log::warn!("WouldBlock {:?}", ep_addr); log::warn!("WouldBlock {:?}", ep_addr);
return Err(UsbError::WouldBlock); return Err(UsbError::WouldBlock);
} }
#[cfg(feature="chain")]
let mut len = 0;
#[cfg(feature="chain")]
for (chain_offset, sub_buf) in buf.chunks_mut(packet_len).enumerate() {
let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
if descriptor.offset() != 0 {
log::info!("appending {:x?}", descriptor);
let available_len = sub_buf.len().min(descriptor.offset());
for (desc_offset, word_chunk) in sub_buf[..available_len].chunks_mut(size_of::<u32>()).enumerate() {
word_chunk.copy_from_slice(
&descriptor.read_data(desc_offset)
.to_le_bytes()[..word_chunk.len()]) // the upper limit index ensures that if sub_buf is an odd number of bytes, we don't panic
}
if sub_buf.len() < descriptor.offset() {
log::warn!("insufficient space in buffer to store incoming data (want: {} have: {}). truncating!", descriptor.offset(), sub_buf.len());
}
len += available_len;
} else {
log::info!("breaking at {:x?}", descriptor);
break;
}
}
#[cfg(not(feature="chain"))]
let mut len = descriptor.offset(); let mut len = descriptor.offset();
if buf.len() < len { if buf.len() < len {
log::error!("read ep{} overflows: {} < {}", ep_addr.index(), buf.len(), len); log::error!("read ep{} overflows: {} < {}", ep_addr.index(), buf.len(), len);
@ -922,10 +1024,12 @@ impl UsbBus for SpinalUsbDevice {
// return Err(UsbError::BufferOverflow) // return Err(UsbError::BufferOverflow)
len = buf.len(); len = buf.len();
} }
#[cfg(not(feature="chain"))]
for (index, dst) in buf[..len].chunks_exact_mut(4).enumerate() { for (index, dst) in buf[..len].chunks_exact_mut(4).enumerate() {
let word = descriptor.read_data(index).to_le_bytes(); let word = descriptor.read_data(index).to_le_bytes();
dst.copy_from_slice(&word); dst.copy_from_slice(&word);
} }
#[cfg(not(feature="chain"))]
if len % 4 != 0 { if len % 4 != 0 {
// this will "overread" the descriptor area, but it's OK because descriptors must be aligned to 16-byte boundaries // this will "overread" the descriptor area, but it's OK because descriptors must be aligned to 16-byte boundaries
// so even if the length is odd, the space allocated will always include dummy padding which will keep us // so even if the length is odd, the space allocated will always include dummy padding which will keep us
@ -938,12 +1042,42 @@ impl UsbBus for SpinalUsbDevice {
} }
// setup for the next transaction // setup for the next transaction
#[cfg(not(feature="chain"))]
ep_status.set_max_packet_size(packet_len as _); ep_status.set_max_packet_size(packet_len as _);
#[cfg(not(feature="chain"))]
descriptor.set_next_desc_and_len(0, buf.len()); descriptor.set_next_desc_and_len(0, buf.len());
#[cfg(not(feature="chain"))]
descriptor.set_desc_flags(UsbDirection::Out, descriptor.set_desc_flags(UsbDirection::Out,
true, true, false); true, true, false);
#[cfg(not(feature="chain"))]
descriptor.set_offset_only(0); // reset the read pointer to 0, also sets in_progress descriptor.set_offset_only(0); // reset the read pointer to 0, also sets in_progress
#[cfg(feature="chain")]
for (chain_offset, sub_buf) in buf.chunks_mut(packet_len).enumerate() {
let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_desc_flags(UsbDirection::Out,
true, true, false);
for (index, _slice) in sub_buf.chunks(size_of::<u32>()).enumerate() {
descriptor.write_data(index, 0); // zeroize the descriptor area so we don't leak data between packets
}
if chain_offset == (max_chain - 1) || sub_buf.len() != packet_len {
// mark the last item in linked list with a null pointer
descriptor.set_next_desc_and_len(0, sub_buf.len());
} else {
// if not the last link, setup the next linked list pointer
let next_addr = self.next_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_next_desc_and_len(next_addr, sub_buf.len());
}
descriptor.set_offset_only(0); // reset the write pointer to 0, also sets in_progress
log::debug!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor);
}
#[cfg(feature="chain")]
ep_status.set_max_packet_size(packet_len as _);
// this is reset to 0 after every transaction by the hardware, so we must reset it
#[cfg(feature="chain")]
ep_status.set_head_offset(head_offset as u32);
// sanity check on interrupts: it should be cleared by this point! // sanity check on interrupts: it should be cleared by this point!
let interrupts = self.regs.interrupts(); let interrupts = self.regs.interrupts();
if interrupts.0 != 0 { if interrupts.0 != 0 {

View file

@ -178,6 +178,9 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
pub fn debug(&self) { pub fn debug(&self) {
log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer);
} }
pub fn get_tag(&self) -> u32 {
self.command_block_wrapper.tag
}
fn max_packet_size(&self) -> u16 { fn max_packet_size(&self) -> u16 {
self.inner.max_packet_size() self.inner.max_packet_size()
@ -188,7 +191,6 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
} }
pub fn read(&mut self) -> Result<(), Error> { pub fn read(&mut self) -> Result<(), Error> {
log::info!("read state: {:?}", self.state);
match self.state { match self.state {
State::WaitingForCommand => self.waiting_for_command(), State::WaitingForCommand => self.waiting_for_command(),
State::ReceivingDataFromHost => self.receiving_data_from_host(), State::ReceivingDataFromHost => self.receiving_data_from_host(),
@ -197,7 +199,6 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
} }
pub fn write(&mut self) -> Result<(), Error> { pub fn write(&mut self) -> Result<(), Error> {
log::info!("write state: {:?}", self.state);
match self.state { match self.state {
State::SendingDataToHost => self.sending_data_to_host(), State::SendingDataToHost => self.sending_data_to_host(),
State::NeedZlp => self.send_zlp(), State::NeedZlp => self.send_zlp(),
@ -242,7 +243,7 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
// logging is enabled or not it appears to sometimes work and sometimes not which makes // logging is enabled or not it appears to sometimes work and sometimes not which makes
// me think it's a timing issue. In the hardware example I'm testing with RTFM appears // me think it's a timing issue. In the hardware example I'm testing with RTFM appears
// to be clearing the // to be clearing the
self.read()?; // self.read()?;
} }
Ok(()) Ok(())
@ -357,11 +358,13 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
/// `take_available` modifies behaviour to return whatever is available, even if that is [u8; 0] /// `take_available` modifies behaviour to return whatever is available, even if that is [u8; 0]
/// returns WouldBlock if there isn't enough data available /// returns WouldBlock if there isn't enough data available
pub fn take_buffered_data(&mut self, len: usize, take_available: bool) -> Result<&[u8], Error> { pub fn take_buffered_data(&mut self, len: usize, take_available: bool) -> Result<&[u8], Error> {
/*
if len > self.buffer.len() { if len > self.buffer.len() {
panic!("BulkOnlyTransport::take_buffered_data called with len > buffer.len() ({} > {}) which can never be successful", panic!("BulkOnlyTransport::take_buffered_data called with len > buffer.len() ({} > {}) which can never be successful",
len, self.buffer.len()); len, self.buffer.len());
} }
log::info!("take_buffered_data() len: {}", len);
let available = self.buffer_i - self.data_i; let available = self.buffer_i - self.data_i;
if !take_available && len > available { if !take_available && len > available {
trace_bot_buffer!("BUFFER> contains insufficient data for take; requested: {}, available: {}", len, self.buffer_i - self.data_i); trace_bot_buffer!("BUFFER> contains insufficient data for take; requested: {}, available: {}", len, self.buffer_i - self.data_i);
@ -379,7 +382,30 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
} }
trace_bot_buffer!("BUFFER> took {}, available after: {}", len, self.buffer_i - self.data_i); trace_bot_buffer!("BUFFER> took {}, available after: {}", len, self.buffer_i - self.data_i);
log::info!("take_buffered_data() s: {} e: {}", s, e);
Ok(&self.buffer[s..e]) Ok(&self.buffer[s..e])
*/
log::info!("take_buffered_data() len: {}", len);
loop {
match self.inner.read_packet(&mut self.buffer[..len]) {
Ok(l) => {
return Ok(&self.buffer[..l]);
}
Err(e) => {
match e {
UsbError::WouldBlock => {
log::info!("bulk read not ready yet");
continue;
},
_ => return Err(Error::UsbError(e)),
}
}
}
}
}
pub fn mark_write_done(&mut self) {
self.command_status_wrapper.data_residue = 0;
self.data_done = true;
} }
pub fn flush(&mut self) -> Result<(), Error> { pub fn flush(&mut self) -> Result<(), Error> {
@ -461,12 +487,9 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
fn end_data_transfer(&mut self) -> Result<(), Error> { fn end_data_transfer(&mut self) -> Result<(), Error> {
// Get the csw ready to send // Get the csw ready to send
//log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); log::info!("end_data_transfer() pack csw for cbw {:x}", self.command_block_wrapper.tag);
self.pack_csw(); self.pack_csw();
log::info!("data_residue: {}", self.command_status_wrapper.data_residue);
self.flush()?; self.flush()?;
//log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer);
log::info!("data_residue: {}", self.command_status_wrapper.data_residue);
// We only send a zero length packet if the last write was a full packet AND we are sending // We only send a zero length packet if the last write was a full packet AND we are sending
// less total bytes than the command header asked for // less total bytes than the command header asked for
@ -478,13 +501,13 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
// send_zlp or flush are called here because we may not get an interrupt in a timley manner // send_zlp or flush are called here because we may not get an interrupt in a timley manner
// if we don't send immediately and // if we don't send immediately and
if needs_zlp { if needs_zlp {
trace_bot_zlp!("ZLP> required"); /*trace_bot_zlp!*/ log::info!("ZLP> required");
self.change_state(State::NeedZlp); self.change_state(State::NeedZlp);
self.send_zlp()?; self.send_zlp()?;
} else { } else {
trace_bot_zlp!("ZLP> not required"); trace_bot_zlp!("ZLP> not required");
self.change_state(State::NeedToSendStatus); self.change_state(State::NeedToSendStatus);
self.flush()?; // self.flush()?;
} }
Ok(()) Ok(())
@ -510,7 +533,7 @@ impl<B: UsbBus> BulkOnlyTransport<'_, B> {
} }
fn check_end_data_transfer(&mut self) -> Result<(), Error> { fn check_end_data_transfer(&mut self) -> Result<(), Error> {
self.flush()?; // self.flush()?;
match self.state { match self.state {
State::ReceivingDataFromHost => { State::ReceivingDataFromHost => {
// Check if we've read everything we were expecting // Check if we've read everything we were expecting

View file

@ -30,10 +30,10 @@ mod logging {
#[cfg(feature = "trace-usb-control")] #[cfg(feature = "trace-usb-control")]
pub use itm_logger::trace as trace_usb_control; pub use itm_logger::trace as trace_usb_control;
pub use log::info as trace_bot_headers; pub use log::debug as trace_bot_headers;
pub use log::info as trace_bot_states; pub use log::info as trace_bot_states;
pub use log::info as trace_bot_bytes; pub use log::debug as trace_bot_bytes;
pub use log::info as trace_bot_zlp; pub use log::debug as trace_bot_zlp;
pub use log::info as trace_bot_buffer; pub use log::debug as trace_bot_buffer;
pub use log::info as trace_usb_control; pub use log::debug as trace_usb_control;
} }

View file

@ -4,7 +4,6 @@ use usb_device::{
}; };
use crate::{ use crate::{
logging::*,
InterfaceSubclass, InterfaceSubclass,
InterfaceProtocol, InterfaceProtocol,
}; };

View file

@ -118,7 +118,7 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
fn process_command(&mut self, new_command: bool) -> Result<CommandState, Error> { fn process_command(&mut self, new_command: bool) -> Result<CommandState, Error> {
use CommandState::*; use CommandState::*;
trace_scsi_command!("COMMAND> {:?}", self.current_command); /*trace_scsi_command!*/log::info!("COMMAND> {:?} tag {:x}", self.current_command, self.inner.get_tag());
Ok(match self.current_command { Ok(match self.current_command {
// No command, nothing to do // No command, nothing to do
@ -195,8 +195,8 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
self.lba_end = r.lba + r.transfer_length - 1; self.lba_end = r.lba + r.transfer_length - 1;
} }
trace_scsi_fs!("FS> Read; new: {}, lba: 0x{:X?}, lba_end: 0x{:X?}, done: {}", /*trace_scsi_fs!*/log::info!("FS> Read; new: {}, lba: 0x{:X?}, lba_end: 0x{:X?}, done: {}, tag: {:x}",
new_command, self.lba, self.lba_end, self.lba == self.lba_end); new_command, self.lba, self.lba_end, self.lba == self.lba_end, self.inner.get_tag());
// We only get here if the buffer is empty // We only get here if the buffer is empty
while self.lba <= self.lba_end { while self.lba <= self.lba_end {
@ -221,8 +221,8 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
self.lba_end = w.lba + w.transfer_length - 1; self.lba_end = w.lba + w.transfer_length - 1;
} }
trace_scsi_fs!("FS> Write; new: {}, lba: 0x{:X?}, lba_end: 0x{:X?}, done: {}", /*trace_scsi_fs!*/log::info!("FS> Write; new: {}, lba: 0x{:X?}, lba_end: 0x{:X?}, done: {}, tag: {:x}",
new_command, self.lba, self.lba_end, self.lba == self.lba_end); new_command, self.lba, self.lba_end, self.lba == self.lba_end, self.inner.get_tag());
let len = match self.inner.transfer_state() { let len = match self.inner.transfer_state() {
TransferState::ReceivingDataFromHost { done: true, full: false, bytes_available: b } => b, TransferState::ReceivingDataFromHost { done: true, full: false, bytes_available: b } => b,
@ -230,13 +230,18 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
_ => BD::BLOCK_BYTES, _ => BD::BLOCK_BYTES,
}; };
let buf = self.inner.take_buffered_data(len, false).expect("Buffer should have enough data"); while self.lba <= self.lba_end {
self.block_device.write_block(self.lba, buf)?; // I think this "len" computation isn't right for our purposes..
self.lba += 1; let buf = self.inner.take_buffered_data(len, true).expect("Buffer should have enough data");
log::info!("buf: {:x?}", &buf[..32]);
self.block_device.write_block(self.lba, buf)?;
self.lba += 1;
}
if self.lba <= self.lba_end { if self.lba <= self.lba_end {
Ongoing Ongoing
} else { } else {
self.inner.mark_write_done();
Done Done
} }
}, },
@ -246,7 +251,7 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
} }
fn receive_command(&mut self) -> Result<(), Error> { fn receive_command(&mut self) -> Result<(), Error> {
let transfer_state = self.inner.transfer_state(); /* let transfer_state = self.inner.transfer_state();
// These calls all assume only a single block will fit in the buffer which // These calls all assume only a single block will fit in the buffer which
// is true here because we configure BOT that way but we could make the inner // is true here because we configure BOT that way but we could make the inner
// buffer length a multiple of BLOCK_SIZE and queue up more than one block // buffer length a multiple of BLOCK_SIZE and queue up more than one block
@ -267,13 +272,15 @@ impl<B: UsbBus, BD: BlockDevice> Scsi<'_, B, BD> {
}; };
if skip { if skip {
log::info!("SKIP condition reached");
Err(UsbError::WouldBlock)?; Err(UsbError::WouldBlock)?;
} }*/
let new_command = self.get_new_command()?; let new_command = self.get_new_command()?;
match self.process_command(new_command) { match self.process_command(new_command) {
Ok(CommandState::Done) => { Ok(CommandState::Done) => {
log::info!("command done");
// Command is done, send CommandOk // Command is done, send CommandOk
self.inner.send_command_ok()?; self.inner.send_command_ok()?;
// Clear the command so we don't try and execute it again // Clear the command so we don't try and execute it again