integrate "chain" fully; use "chain" feature only for bulk endpoints

bulk endpoints have a different behavior now than interrupt
endpoints; bulk endpoints have chained descriptors so that the
hardware automatically responds to multiple USB packets quickly.

interrupt endpoints also have subtly different way of handling
data readiness, which is incompatible with the block device layer
as implemented.
This commit is contained in:
bunnie 2023-01-14 00:09:16 +08:00
parent 7d641641cd
commit 26a3adcc01
2 changed files with 88 additions and 96 deletions

View file

@ -49,8 +49,7 @@ rand_chacha = "0.3.1"
precursor = ["utralib/precursor"] precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"] hosted = ["utralib/hosted"]
renode = ["utralib/renode"] renode = ["utralib/renode"]
chain = []
minimal = ["mass-storage"] minimal = ["mass-storage"]
mass-storage = ["usbd_scsi", "usbd_mass_storage"] mass-storage = ["usbd_scsi", "usbd_mass_storage"]
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 = ["chain"] default = []

View file

@ -555,7 +555,20 @@ impl UsbBus for SpinalUsbDevice {
// now commit the ep config // now commit the ep config
self.status_write_volatile(index, ep_status); self.status_write_volatile(index, ep_status);
self.view.ep_allocs[index] = Some((offset as usize / 16, base_packet_size as usize, CHAINED_BUF_LEN / base_packet_size as usize)); match ep_type {
EndpointType::Bulk => { // only Bulk types do chaining
self.view.ep_allocs[index] = Some(
(offset as usize / 16,
base_packet_size as usize,
CHAINED_BUF_LEN / base_packet_size as usize))
}
_ => {
self.view.ep_allocs[index] = Some(
(offset as usize / 16,
base_packet_size as usize,
1));
}
}
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
log::info!("alloc ep{} type {:?} dir {:?}, @{:x}({})", log::info!("alloc ep{} type {:?} dir {:?}, @{:x}({})",
@ -898,20 +911,6 @@ impl UsbBus for SpinalUsbDevice {
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
self.read_allowed.store(masked, Ordering::SeqCst); self.read_allowed.store(masked, Ordering::SeqCst);
} }
/*
let mut ep_status = self.status_read_volatile(ep_addr.index());
let start = self.tt.elapsed_ms();
let mut now = start;
while (ep_status.head_offset() != 0) && (now - start < WRITE_TIMEOUT_MS) {
xous::yield_slice();
ep_status = self.status_read_volatile(ep_addr.index());
now = self.tt.elapsed_ms();
}
if now - start >= WRITE_TIMEOUT_MS {
log::debug!("read wait timed out: head offset {:x?} {:x?}", ep_status.head_offset() * 16,
self.descriptor_from_status(&ep_status));
}
*/
let mut ep_status = self.status_read_volatile(ep_addr.index()); let mut ep_status = self.status_read_volatile(ep_addr.index());
// log::info!("head_offset{}: {:x}", ep_addr.index(), head_offset * 16); // log::info!("head_offset{}: {:x}", ep_addr.index(), head_offset * 16);
@ -925,9 +924,13 @@ impl UsbBus for SpinalUsbDevice {
return Err(UsbError::WouldBlock); return Err(UsbError::WouldBlock);
} }
self.udc_hard_halt(ep_addr.index()); self.udc_hard_halt(ep_addr.index());
#[cfg(feature="chain")]
let mut len = 0; let mut len = if max_chain > 1 {
#[cfg(feature="chain")] 0
} else {
descriptor.offset()
};
if max_chain > 1 { // chained descriptors
for (chain_offset, sub_buf) in buf.chunks_mut(packet_len).enumerate() { 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); let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
if descriptor.offset() != 0 { if descriptor.offset() != 0 {
@ -947,43 +950,10 @@ impl UsbBus for SpinalUsbDevice {
break; break;
} }
} }
#[cfg(not(feature="chain"))]
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);
// just return a truncated set of data
// 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() {
let word = descriptor.read_data(index).to_le_bytes();
dst.copy_from_slice(&word);
}
#[cfg(not(feature="chain"))]
if len % 4 != 0 {
// 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
// from reading into neighboring data.
let word = descriptor.read_data(len / 4).to_le_bytes();
// write only into the portion of the buffer that's allocated, don't write the extra 0's
for i in 0..len % 4 {
buf[(len / 4) + i] = word[i]
}
}
// setup for the next transaction
#[cfg(not(feature="chain"))]
ep_status.set_max_packet_size(packet_len as _);
#[cfg(not(feature="chain"))]
descriptor.set_next_desc_and_len(0, buf.len());
#[cfg(not(feature="chain"))]
descriptor.set_desc_flags(UsbDirection::Out,
true, true, false);
#[cfg(not(feature="chain"))]
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() { 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); let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len);
descriptor.set_desc_flags(UsbDirection::Out, descriptor.set_desc_flags(UsbDirection::Out,
@ -991,7 +961,6 @@ impl UsbBus for SpinalUsbDevice {
for (index, _slice) in sub_buf.chunks(size_of::<u32>()).enumerate() { 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 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 { if chain_offset == (max_chain - 1) || sub_buf.len() != packet_len {
// mark the last item in linked list with a null pointer // mark the last item in linked list with a null pointer
descriptor.set_next_desc_and_len(0, sub_buf.len()); descriptor.set_next_desc_and_len(0, sub_buf.len());
@ -1003,11 +972,35 @@ impl UsbBus for SpinalUsbDevice {
descriptor.set_offset_only(0); // reset the write pointer to 0, also sets in_progress descriptor.set_offset_only(0); // reset the write pointer to 0, also sets in_progress
log::debug!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor); log::debug!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor);
} }
#[cfg(feature="chain")]
ep_status.set_max_packet_size(packet_len as _); 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 // 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); ep_status.set_head_offset(head_offset as u32);
} else { // single descriptor
if buf.len() < len {
log::error!("read ep{} overflows: {} < {}", ep_addr.index(), buf.len(), len);
len = buf.len();
}
for (index, dst) in buf[..len].chunks_exact_mut(4).enumerate() {
let word = descriptor.read_data(index).to_le_bytes();
dst.copy_from_slice(&word);
}
if len % 4 != 0 {
// 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
// from reading into neighboring data.
let word = descriptor.read_data(len / 4).to_le_bytes();
// write only into the portion of the buffer that's allocated, don't write the extra 0's
for i in 0..len % 4 {
buf[(len / 4) + i] = word[i]
}
}
// setup for the next transaction
ep_status.set_max_packet_size(packet_len as _);
descriptor.set_next_desc_and_len(0, buf.len());
descriptor.set_desc_flags(UsbDirection::Out,
true, true, false);
descriptor.set_offset_only(0); // reset the read pointer to 0, also sets in_progress
}
// 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();