mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
significant code cleanup on the usb driver
- endpoint allocator now handles more edge cases. likely there are still more weird cases to be considered, but it at least handles the CDC-ACM case of a lone interrupt endpoint that can't be mix-and-matched with other devices - initiate OUT to kick off reads - remove dead code - handle bulk and interrupt cases for both read and write - clean up reset edge cases - still more work to do there, the stack does not recover on second plugin - better debug output
This commit is contained in:
parent
9609a6d66e
commit
faaa1ad4b9
1 changed files with 153 additions and 386 deletions
|
|
@ -10,6 +10,8 @@ use bitfield::bitfield;
|
|||
#[cfg(feature = "std")]
|
||||
use usb_device::bus::PollResult;
|
||||
#[cfg(feature = "std")]
|
||||
use usb_device::endpoint::EndpointType;
|
||||
#[cfg(feature = "std")]
|
||||
use usb_device::{Result, UsbDirection, class_prelude::*};
|
||||
#[cfg(feature = "std")]
|
||||
use utralib::generated::*;
|
||||
|
|
@ -197,6 +199,21 @@ impl TryFrom<u8> for EpType {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "std")]
|
||||
impl Into<EndpointType> for EpType {
|
||||
fn into(self) -> EndpointType {
|
||||
match self {
|
||||
Self::BulkInbound => EndpointType::Bulk,
|
||||
Self::BulkOutbound => EndpointType::Bulk,
|
||||
Self::IntrInbound => EndpointType::Interrupt,
|
||||
Self::IntrOutbound => EndpointType::Interrupt,
|
||||
Self::IsochInbound => EndpointType::Isochronous,
|
||||
Self::IsochOutbound => EndpointType::Isochronous,
|
||||
Self::ControlOrInvalid => EndpointType::Control,
|
||||
Self::Invalid2 => EndpointType::Control,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bitfield! {
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Default)]
|
||||
|
|
@ -870,10 +887,10 @@ pub struct CorigineUsb {
|
|||
|
||||
// event handler. Allows for divergence between no-std and std environments.
|
||||
handler: Option<fn(&mut Self, &mut EventTrbS) -> CrgEvent>,
|
||||
event_inner: Option<CrgEvent>,
|
||||
pub event_inner: Option<CrgEvent>,
|
||||
// data pointer of the current TRB to the application layer. Tuple is (addr, len).
|
||||
// we form the unsafe slice later depending on if the application needs mutability or not :-/
|
||||
app_ptr: Option<AppPtr>,
|
||||
pub app_ptr: Option<AppPtr>,
|
||||
|
||||
pub state: UsbDeviceState,
|
||||
pub cur_interface_num: u8,
|
||||
|
|
@ -1091,7 +1108,7 @@ impl CorigineUsb {
|
|||
+ ((ep_num - 1) as usize * 2 + if dir { 1 } else { 0 }) * CRG_UDC_APP_BUF_LEN
|
||||
+ enq_index;
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("ep0 app_ptr: {:x} index {}", addr, enq_index);
|
||||
crate::println!("ep{} app_ptr: {:x} index {}", ep_num, addr, enq_index);
|
||||
Some(addr)
|
||||
} else {
|
||||
crate::println!("ep_num {} is out of range", ep_num);
|
||||
|
|
@ -1208,6 +1225,7 @@ impl CorigineUsb {
|
|||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
crate::println!("~~~~~~~~~~~~~~~~INIT~~~~~~~~~~~~~~~");
|
||||
let ifram_slice = unsafe {
|
||||
core::slice::from_raw_parts_mut(
|
||||
self.ifram_base_ptr as *mut u32,
|
||||
|
|
@ -1709,7 +1727,12 @@ impl CorigineUsb {
|
|||
pub fn ep0_send(&mut self, addr: usize, len: usize, intr_target: u32) {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
unsafe {
|
||||
crate::println!("ep0 send ({}) {:x?}", len, core::slice::from_raw_parts(addr as *const u8, len));
|
||||
crate::println!(
|
||||
"ep0 send ({}) {:x?} tag {}",
|
||||
len,
|
||||
core::slice::from_raw_parts(addr as *const u8, len),
|
||||
self.setup_tag
|
||||
);
|
||||
}
|
||||
let mut enq_pt =
|
||||
unsafe { self.udc_ep[0].enq_pt.load(Ordering::SeqCst).as_mut().expect("couldn't deref pointer") };
|
||||
|
|
@ -1739,6 +1762,7 @@ impl CorigineUsb {
|
|||
}
|
||||
|
||||
pub fn ep0_enqueue(&mut self, addr: usize, len: usize, intr_target: u32) {
|
||||
crate::println!("ep0 enqueue ({}) tag {}", len, self.setup_tag);
|
||||
let enq_pt =
|
||||
unsafe { self.udc_ep[0].enq_pt.load(Ordering::SeqCst).as_mut().expect("couldn't deref pointer") };
|
||||
enq_pt.control_data_trb(
|
||||
|
|
@ -2201,11 +2225,6 @@ impl CorigineUsb {
|
|||
#[cfg(feature = "std")]
|
||||
pub struct CorigineWrapper {
|
||||
pub hw: Arc<Mutex<CorigineUsb>>,
|
||||
/// The hardware stack works with endpoints mapped as "PEI", where IN/OUT are paired
|
||||
/// into a single index and the meaning of IN and OUT are fixed based on the position in
|
||||
/// the index. The allocator as written cannot handle devices allocated with a `None`
|
||||
/// EP specifier while also having only either an IN or an OUT EP (but not the other).
|
||||
pub free_pei: usize,
|
||||
/// Tuple is (type of endpoint, max packet size)
|
||||
pub ep_meta: [Option<(EpType, usize)>; CRG_EP_NUM],
|
||||
pub ep_out_ready: Box<[AtomicBool]>,
|
||||
|
|
@ -2217,7 +2236,6 @@ impl CorigineWrapper {
|
|||
pub fn new(obj: CorigineUsb) -> Self {
|
||||
let c = Self {
|
||||
hw: Arc::new(Mutex::new(obj)),
|
||||
free_pei: 2,
|
||||
ep_meta: [None; CRG_EP_NUM],
|
||||
ep_out_ready: (0..CRG_EP_NUM + 1)
|
||||
.map(|_| AtomicBool::new(false))
|
||||
|
|
@ -2232,7 +2250,6 @@ impl CorigineWrapper {
|
|||
pub fn clone(&self) -> Self {
|
||||
let mut c = Self {
|
||||
hw: self.hw.clone(),
|
||||
free_pei: 2,
|
||||
ep_meta: [None; CRG_EP_NUM],
|
||||
ep_out_ready: (0..CRG_EP_NUM + 1)
|
||||
.map(|_| AtomicBool::new(false))
|
||||
|
|
@ -2249,8 +2266,10 @@ impl CorigineWrapper {
|
|||
}
|
||||
|
||||
pub fn core(&self) -> std::sync::MutexGuard<'_, CorigineUsb> {
|
||||
/*
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("lock status: {}", if self.hw.try_lock().is_err() { "locked " } else { "unlocked" });
|
||||
*/
|
||||
self.hw.lock().unwrap()
|
||||
}
|
||||
}
|
||||
|
|
@ -2297,18 +2316,54 @@ impl UsbBus for CorigineWrapper {
|
|||
_interval: u8,
|
||||
) -> Result<EndpointAddress> {
|
||||
// #[cfg(feature = "verbose-debug")]
|
||||
crate::println!("alloc_ep {:?} size: {} dir: {:?}", ep_addr, max_packet_size, ep_dir);
|
||||
crate::println!(
|
||||
"alloc_ep {:?} size: {} dir: {:?} type: {:?}",
|
||||
ep_addr,
|
||||
max_packet_size,
|
||||
ep_dir,
|
||||
ep_type
|
||||
);
|
||||
|
||||
let allocated_ep = if let Some(addr) = ep_addr {
|
||||
addr.index()
|
||||
} else {
|
||||
let ep = self.free_pei / 2;
|
||||
self.free_pei += 1;
|
||||
ep
|
||||
// PEI layout is like this:
|
||||
// 0 EP1OUT
|
||||
// 1 EP1IN
|
||||
// 2 EP2OUT
|
||||
// 3 EP2IN
|
||||
let mut ep_search: Option<usize> = None;
|
||||
for candidate in (0..self.ep_meta.len()).step_by(2) {
|
||||
if self.ep_meta[candidate].is_none() && self.ep_meta[candidate].is_none() {
|
||||
ep_search = Some((candidate / 2) + 1);
|
||||
break;
|
||||
} else {
|
||||
if candidate + 4 <= self.ep_meta.len() {
|
||||
// skip "holes" in the allocation
|
||||
if self.ep_meta[candidate + 2].is_some() || self.ep_meta[candidate + 3].is_some() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some((existing_type, _)) = self.ep_meta[candidate] {
|
||||
if self.ep_meta[candidate + 1].is_none()
|
||||
&& <EpType as Into<EndpointType>>::into(existing_type) == ep_type
|
||||
{
|
||||
ep_search = Some((candidate / 2) + 1);
|
||||
break;
|
||||
}
|
||||
} else if let Some((existing_type, _)) = self.ep_meta[candidate + 1] {
|
||||
if self.ep_meta[candidate].is_none()
|
||||
&& <EpType as Into<EndpointType>>::into(existing_type) == ep_type
|
||||
{
|
||||
ep_search = Some((candidate / 2) + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(ep) = ep_search { ep } else { return Err(UsbError::EndpointOverflow) }
|
||||
};
|
||||
if allocated_ep > (CRG_EP_NUM / 2) + 1 {
|
||||
return Err(UsbError::EndpointOverflow);
|
||||
}
|
||||
crate::println!("allocated ep {}", allocated_ep);
|
||||
|
||||
let dir = match ep_dir {
|
||||
UsbDirection::Out => CRG_OUT,
|
||||
|
|
@ -2345,6 +2400,7 @@ impl UsbBus for CorigineWrapper {
|
|||
if allocated_ep != 0 {
|
||||
// also record metadata for non-0 EPs
|
||||
self.ep_meta[pei - 2] = Some((hw_ep_type, max_packet_size as usize));
|
||||
crate::println!("ep_meta[{}]: {:?}", pei - 2, self.ep_meta[pei - 2]);
|
||||
}
|
||||
|
||||
Ok(EndpointAddress::from_parts(allocated_ep, ep_dir))
|
||||
|
|
@ -2376,6 +2432,10 @@ impl UsbBus for CorigineWrapper {
|
|||
// IRQ enable must happen without dependency on the hardware lock
|
||||
hw.irq_csr.clone()
|
||||
};
|
||||
// reset the ready state
|
||||
for ready in self.ep_out_ready.iter() {
|
||||
ready.store(false, Ordering::SeqCst);
|
||||
}
|
||||
// the lock is released, now we can enable irqs
|
||||
irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, 0xffff_ffff); // blanket clear
|
||||
irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 3); // FIXME: hard coded value that enables CORIGINE_IRQ_MASK | SW_IRQ_MASK
|
||||
|
|
@ -2400,56 +2460,6 @@ impl UsbBus for CorigineWrapper {
|
|||
let dir = CorigineUsb::pei_to_dir(pei);
|
||||
crate::println!("enabling pei {}, dir {:?}", pei, dir);
|
||||
self.core().ep_enable(CorigineUsb::pei_to_ep(pei), dir, max_packet_size as u16, hw_ep_type);
|
||||
|
||||
// If the end point is an OUT, set up a standing transfer to receive the incoming packet
|
||||
/*
|
||||
if dir == CRG_OUT {
|
||||
crate::println!("setting up standing transfers");
|
||||
let addr = self
|
||||
.core()
|
||||
.get_app_buf_ptr(CorigineUsb::pei_to_ep(pei), dir)
|
||||
.expect("should always be a buffer available at set_address");
|
||||
crate::println!("app pointer at {:x}", addr);
|
||||
match hw_ep_type {
|
||||
// NOTE: we kind of need to know how big of a transfer we expect for this
|
||||
// to work, otherwise, the transfer may not complete. But we won't know
|
||||
// this until "read" is called by the USB stack implementation, so...how to
|
||||
// break this dependency??
|
||||
//
|
||||
// For now the code just sets the size to the whole buffer size but this should
|
||||
// cause the stack to not respond to packets that fail to meet the total expected
|
||||
// length.
|
||||
EpType::BulkOutbound => {
|
||||
crate::println!("enabling bulk out");
|
||||
self.core().bulk_xfer(
|
||||
CorigineUsb::pei_to_ep(pei),
|
||||
dir,
|
||||
addr,
|
||||
CRG_UDC_APP_BUF_LEN,
|
||||
CRG_INT_TARGET,
|
||||
CRG_XFER_SET_CHAIN,
|
||||
);
|
||||
}
|
||||
EpType::BulkInbound => {
|
||||
crate::println!("bulk in UNREACHABLE");
|
||||
unreachable!("Bulk inbound not reachable for OUT endpoints")
|
||||
}
|
||||
_ => {
|
||||
crate::println!("enabling interrupt pei {}", pei);
|
||||
self.core().ep_xfer(
|
||||
CorigineUsb::pei_to_ep(pei),
|
||||
dir,
|
||||
addr,
|
||||
CRG_UDC_APP_BUF_LEN,
|
||||
CRG_INT_TARGET,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
let pei = index + 2;
|
||||
let dir = CorigineUsb::pei_to_dir(pei);
|
||||
|
|
@ -2500,14 +2510,15 @@ impl UsbBus for CorigineWrapper {
|
|||
// TODO: resolve ep_addr to an ep_type, so we can handle both bulk and intr. But for now, let's
|
||||
// just do intr since that's all we're using for testing.
|
||||
|
||||
// #[cfg(feature = "verbose-debug")]
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(
|
||||
" ******** WRITE: {:?}({}): {:x?}",
|
||||
ep_addr.index(),
|
||||
buf.len(),
|
||||
&buf[..8.min(buf.len())]
|
||||
);
|
||||
let addr = if let Some(addr) = self.core().get_app_buf_ptr(ep_addr.index() as u8, USB_RECV) {
|
||||
let addr = if let Some(addr) = self.core().get_app_buf_ptr(ep_addr.index() as u8, CRG_IN) {
|
||||
// crate::println!("addr {:x}", addr);
|
||||
addr
|
||||
} else {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
|
|
@ -2517,16 +2528,31 @@ impl UsbBus for CorigineWrapper {
|
|||
let hw_buf = unsafe { core::slice::from_raw_parts_mut(addr as *mut u8, CRG_UDC_APP_BUF_LEN) };
|
||||
assert!(buf.len() < CRG_UDC_APP_BUF_LEN, "write buffer size exceeded");
|
||||
hw_buf[..buf.len()].copy_from_slice(&buf);
|
||||
self.core().ep_xfer(
|
||||
ep_addr.index() as u8,
|
||||
CRG_IN,
|
||||
addr,
|
||||
buf.len(),
|
||||
CRG_INT_TARGET,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
let pei = CorigineUsb::pei(ep_addr.index() as u8, CRG_IN);
|
||||
if let Some((hw_ep_type, _max_packet_size)) = self.ep_meta[pei - 2] {
|
||||
match hw_ep_type {
|
||||
EpType::BulkInbound => {
|
||||
self.core().bulk_xfer(ep_addr.index() as u8, CRG_IN, addr, buf.len(), 0, 0);
|
||||
}
|
||||
EpType::IntrInbound => {
|
||||
self.core().ep_xfer(
|
||||
ep_addr.index() as u8,
|
||||
CRG_IN,
|
||||
addr,
|
||||
buf.len(),
|
||||
CRG_INT_TARGET,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
todo!("EP type not handled yet {:?}", hw_ep_type);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("attempt to access EP{} with no meta[{}] mapping", ep_addr.index(), pei - 2)
|
||||
}
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("ep{} initiated {}", ep_addr.index(), buf.len());
|
||||
Ok(buf.len())
|
||||
|
|
@ -2566,9 +2592,9 @@ impl UsbBus for CorigineWrapper {
|
|||
}
|
||||
} else {
|
||||
if self.address_is_set.load(Ordering::SeqCst) {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(" ******** READ {}", ep_addr.index());
|
||||
let pending_ep = self.core().pending_ep();
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(" ******** READ {} pending {:?}", ep_addr.index(), pending_ep);
|
||||
let ret = if let Some(pending_ep) = pending_ep {
|
||||
if pending_ep as usize == ep_addr.index() {
|
||||
let app_ptr = self.core().app_ptr.take().expect("inconistent app_ptr state");
|
||||
|
|
@ -2608,7 +2634,8 @@ impl UsbBus for CorigineWrapper {
|
|||
.expect("should always be a buffer available at set_address")
|
||||
};
|
||||
// crate::println!("address {:x}", addr);
|
||||
if let Some((hw_ep_type, max_packet_size)) = self.ep_meta[ep_addr.index()] {
|
||||
let pei = CorigineUsb::pei(ep_addr.index() as u8, CRG_OUT);
|
||||
if let Some((hw_ep_type, max_packet_size)) = self.ep_meta[pei - 2] {
|
||||
match hw_ep_type {
|
||||
// NOTE: we kind of need to know how big of a transfer we expect for this
|
||||
// to work, otherwise, the transfer may not complete. But we won't know
|
||||
|
|
@ -2619,13 +2646,19 @@ impl UsbBus for CorigineWrapper {
|
|||
// cause the stack to not respond to packets that fail to meet the total expected
|
||||
// length.
|
||||
EpType::BulkOutbound => {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(
|
||||
"::::::: setting up bulk outbound ep{} @ {:x} :::::::",
|
||||
ep_addr.index(),
|
||||
addr
|
||||
);
|
||||
self.core().bulk_xfer(
|
||||
ep_addr.index() as u8,
|
||||
CRG_OUT,
|
||||
addr,
|
||||
CRG_UDC_APP_BUF_LEN.min(buf.len()).min(max_packet_size),
|
||||
CRG_INT_TARGET,
|
||||
CRG_XFER_SET_CHAIN,
|
||||
0, // CRG_XFER_SET_CHAIN,
|
||||
);
|
||||
}
|
||||
EpType::BulkInbound => {
|
||||
|
|
@ -2633,6 +2666,12 @@ impl UsbBus for CorigineWrapper {
|
|||
unreachable!("Bulk inbound not reachable for OUT endpoints");
|
||||
}
|
||||
_ => {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(
|
||||
"::::::: setting up interrupt outbound ep{} @ {:x} :::::::",
|
||||
ep_addr.index(),
|
||||
addr
|
||||
);
|
||||
// crate::println!("ep_type {:?}", hw_ep_type);
|
||||
self.core().ep_xfer(
|
||||
ep_addr.index() as u8,
|
||||
|
|
@ -2703,26 +2742,17 @@ impl UsbBus for CorigineWrapper {
|
|||
/// interrupt handler. See the [`PollResult`] struct for more information.
|
||||
fn poll(&self) -> PollResult {
|
||||
match self.core().event_inner.take() {
|
||||
Some(e) => {
|
||||
match e {
|
||||
CrgEvent::None => PollResult::None,
|
||||
CrgEvent::Connect => {
|
||||
/*
|
||||
let mut hw = self.hw.lock().unwrap();
|
||||
hw.reset();
|
||||
hw.init();
|
||||
hw.start(); */
|
||||
PollResult::Reset
|
||||
}
|
||||
CrgEvent::Data(ep_out, ep_in_complete, ep_setup) => {
|
||||
PollResult::Data { ep_out, ep_in_complete, ep_setup }
|
||||
}
|
||||
CrgEvent::Error => {
|
||||
crate::println!("Error detected in poll, issuing reset");
|
||||
PollResult::Reset
|
||||
}
|
||||
Some(e) => match e {
|
||||
CrgEvent::None => PollResult::None,
|
||||
CrgEvent::Connect => PollResult::Reset,
|
||||
CrgEvent::Data(ep_out, ep_in_complete, ep_setup) => {
|
||||
PollResult::Data { ep_out, ep_in_complete, ep_setup }
|
||||
}
|
||||
}
|
||||
CrgEvent::Error => {
|
||||
crate::println!("Error detected in poll, issuing reset");
|
||||
PollResult::Reset
|
||||
}
|
||||
},
|
||||
None => PollResult::None,
|
||||
}
|
||||
}
|
||||
|
|
@ -2755,6 +2785,10 @@ impl UsbBus for CorigineWrapper {
|
|||
// IRQ enable must happen without dependency on the hardware lock
|
||||
hw.irq_csr.clone()
|
||||
};
|
||||
// reset the ready state
|
||||
for ready in self.ep_out_ready.iter() {
|
||||
ready.store(false, Ordering::SeqCst);
|
||||
}
|
||||
// the lock is released, now we can enable irqs
|
||||
irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, 0xffff_ffff); // blanket clear
|
||||
irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 3); // FIXME: hard coded value that enables CORIGINE_IRQ_MASK | SW_IRQ_MASK
|
||||
|
|
@ -2763,276 +2797,10 @@ impl UsbBus for CorigineWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("handle_event: {:x?}", event_trb);
|
||||
let pei = event_trb.get_endpoint_id();
|
||||
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() {
|
||||
TrbType::EventPortStatusChange => {
|
||||
let portsc_val = this.csr.r(PORTSC);
|
||||
this.csr.wo(PORTSC, portsc_val);
|
||||
// this.print_status(portsc_val);
|
||||
|
||||
let portsc = PortSc(portsc_val);
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("{:?}", portsc);
|
||||
|
||||
if portsc.prc() && !portsc.pr() {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("update_current_speed() - reset done");
|
||||
this.update_current_speed();
|
||||
}
|
||||
if portsc.csc() && portsc.ppc() && portsc.pp() && portsc.ccs() {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("update_current_speed() - cable connect");
|
||||
this.update_current_speed();
|
||||
}
|
||||
/*
|
||||
let cs = (portsc_val & this.csr.ms(PORTSC_CCS, 1)) != 0;
|
||||
let pp = (portsc_val & this.csr.ms(PORTSC_PP, 1)) != 0;
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" {:x} {:x?} PORT_STATUS_CHANGE", portsc_val, event_trb.dw3);
|
||||
|
||||
if portsc_val & this.csr.ms(PORTSC_CSC, 1) != 0 {
|
||||
if cs {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Port connection");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Port connection");
|
||||
// ret = CrgEvent::Connect;
|
||||
} else {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Port disconnection");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Port disconnection");
|
||||
}
|
||||
}
|
||||
|
||||
if portsc_val & this.csr.ms(PORTSC_PPC, 1) != 0 {
|
||||
if pp {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Power present");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Power present");
|
||||
// ret = CrgEvent::None;
|
||||
} else {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Power not present");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Power not present");
|
||||
}
|
||||
}
|
||||
|
||||
if (portsc_val & this.csr.ms(PORTSC_CSC, 1) != 0)
|
||||
&& (portsc_val & this.csr.ms(PORTSC_PPC, 1) != 0)
|
||||
{
|
||||
if cs && pp {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Cable connect and power present");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Cable connect and power present");
|
||||
this.update_current_speed();
|
||||
// ret = CrgEvent::None;
|
||||
}
|
||||
}
|
||||
|
||||
if (portsc_val & this.csr.ms(PORTSC_PRC, 1)) != 0 {
|
||||
if portsc_val & this.csr.ms(PORTSC_PR, 1) != 0 {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" In port reset process");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" In port reset process");
|
||||
} else {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Port reset done");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Port reset done");
|
||||
this.update_current_speed();
|
||||
ret = CrgEvent::Connect;
|
||||
}
|
||||
}
|
||||
|
||||
if (portsc_val & this.csr.ms(PORTSC_PLC, 1)) != 0 {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" Port link state change: {:?}", PortLinkState::from_portsc(portsc_val));
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" Port link state change: {:?}", PortLinkState::from_portsc(portsc_val));
|
||||
}
|
||||
|
||||
if !cs && !pp {
|
||||
#[cfg(not(feature = "std"))]
|
||||
println!(" cable disconnect and power not present");
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!(" cable disconnect and power not present");
|
||||
}
|
||||
*/
|
||||
this.csr.rmwf(EVENTCONFIG_SETUP_ENABLE, 1);
|
||||
}
|
||||
TrbType::EventTransfer => {
|
||||
let comp_code =
|
||||
CompletionCode::try_from(event_trb.dw2.compl_code()).expect("Invalid completion code");
|
||||
|
||||
// update the dequeue pointer
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("event_transfer {:x?}", event_trb);
|
||||
let deq_pt =
|
||||
unsafe { (event_trb.dw0 as *mut TransferTrbS).add(1).as_mut().expect("Couldn't deref ptr") };
|
||||
if deq_pt.get_trb_type() == TrbType::Link {
|
||||
udc_ep.deq_pt = AtomicPtr::new(udc_ep.first_trb.load(Ordering::SeqCst));
|
||||
} else {
|
||||
udc_ep.deq_pt = AtomicPtr::new(deq_pt as *mut TransferTrbS);
|
||||
}
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("EventTransfer: comp_code {:?}, PEI {}", comp_code, pei);
|
||||
|
||||
let dir = (pei & 1) != 0;
|
||||
if pei == 0 {
|
||||
if comp_code == CompletionCode::Success {
|
||||
// ep0_xfer_complete
|
||||
if dir == USB_SEND {
|
||||
ret = CrgEvent::Data(0, 1, 0); // FIXME: this ordering contradicts the `dir` bit, but seems necessary to trigger the next packet send
|
||||
} else {
|
||||
ret = CrgEvent::Data(1, 0, 0); // FIXME: this ordering contradicts the `dir` bit, but seems necessary to trigger the next packet send
|
||||
}
|
||||
} else {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("EP0 unhandled comp_code: {:?}", comp_code);
|
||||
ret = CrgEvent::None;
|
||||
}
|
||||
} else if pei >= 2 {
|
||||
if comp_code == CompletionCode::Success || comp_code == CompletionCode::ShortPacket {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("EP{} xfer event, dir {}", ep_num, if dir { "OUT" } else { "IN" });
|
||||
// xfer_complete
|
||||
if dir == CRG_OUT {
|
||||
let addr = this.retire_app_buf_ptr(ep_num, dir);
|
||||
let mps =
|
||||
this.max_packet_size[pei as usize].expect("max packet size was not initialized!");
|
||||
let hw_buf = unsafe { core::slice::from_raw_parts_mut(addr as *mut u8, mps) };
|
||||
// copy the whole hardware buffer contents -- even if it's bogus
|
||||
let mut storage = [0u8; CRG_UDC_APP_BUF_LEN];
|
||||
storage[..mps].copy_from_slice(hw_buf);
|
||||
this.readout[ep_num as usize - 1] = Some(storage);
|
||||
// re-enqueue the listener
|
||||
let addr =
|
||||
this.get_app_buf_ptr(ep_num, dir).expect("retire should have opened an entry");
|
||||
this.ep_xfer(
|
||||
ep_num,
|
||||
dir,
|
||||
addr,
|
||||
CRG_UDC_APP_BUF_LEN,
|
||||
CRG_INT_TARGET,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
ret = CrgEvent::Data(ep_num as u16, 0, 0);
|
||||
} else {
|
||||
this.retire_app_buf_ptr(ep_num, dir);
|
||||
ret = CrgEvent::Data(0, ep_num as u16, 0);
|
||||
}
|
||||
} else if comp_code == CompletionCode::MissedServiceError {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("MissedServiceError");
|
||||
} else {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("EventTransfer {:?} event not handled", comp_code);
|
||||
}
|
||||
}
|
||||
}
|
||||
TrbType::SetupPkt => {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(" handle_setup_pkt");
|
||||
let mut setup_storage = [0u8; 8];
|
||||
setup_storage.copy_from_slice(&event_trb.get_raw_setup());
|
||||
this.setup = Some(setup_storage);
|
||||
this.setup_tag = event_trb.get_setup_tag();
|
||||
|
||||
// demo of setup packets working in loader
|
||||
#[cfg(not(feature = "std"))]
|
||||
{
|
||||
let _request_type = setup_storage[0];
|
||||
let request = setup_storage[1];
|
||||
let value = u16::from_le_bytes(setup_storage[2..4].try_into().unwrap());
|
||||
let _index = u16::from_le_bytes(setup_storage[4..6].try_into().unwrap());
|
||||
let _length = u16::from_le_bytes(setup_storage[6..].try_into().unwrap());
|
||||
|
||||
const SET_ADDRESS: u8 = 5;
|
||||
const GET_DESCRIPTOR: u8 = 6;
|
||||
|
||||
match request {
|
||||
SET_ADDRESS => {
|
||||
this.set_addr(value as u8, 0);
|
||||
println!("address set");
|
||||
}
|
||||
GET_DESCRIPTOR => {
|
||||
let base_ptr = crate::usb::driver::CRG_UDC_MEMBASE + CRG_UDC_EP0_BUF_OFFSET;
|
||||
let ep0_buf = base_ptr as *mut u8;
|
||||
let desc = [
|
||||
0x12u8, 0x1, 0x10, 0x2, 0, 0, 0, 0x8, // pkt 0
|
||||
0x9, 0x12, 0x13, 0x36, 0x10, 0, 0x1, 0x2, // pkt 1
|
||||
0x3, 0x1, // pkt 2
|
||||
];
|
||||
// [12, 1, 10, 2, 0, 0, 0, 8]
|
||||
println!("ep0 send {}", desc.len());
|
||||
let mut enq_pt = unsafe {
|
||||
this.udc_ep[0]
|
||||
.enq_pt
|
||||
.load(Ordering::SeqCst)
|
||||
.as_mut()
|
||||
.expect("couldn't deref pointer")
|
||||
};
|
||||
let mut pcs = this.udc_ep[0].pcs;
|
||||
let tag = this.setup_tag;
|
||||
for (j, chunk) in desc.chunks(8).enumerate() {
|
||||
for (i, &d) in chunk.iter().enumerate() {
|
||||
unsafe { ep0_buf.add(i + j * 8).write_volatile(d) };
|
||||
}
|
||||
// this.ep0_send(base_ptr, chunk.len(), 0);
|
||||
enq_pt.control_data_trb(
|
||||
unsafe { ep0_buf.add(j * 8) } as u32,
|
||||
pcs,
|
||||
1,
|
||||
chunk.len() as u32,
|
||||
0,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
tag,
|
||||
CRG_INT_TARGET,
|
||||
);
|
||||
(enq_pt, pcs) = this.udc_ep[0].increment_enq_pt();
|
||||
// this.knock_doorbell(0);
|
||||
compiler_fence(Ordering::SeqCst);
|
||||
this.csr.wfo(DOORBELL_TARGET, 0);
|
||||
}
|
||||
enq_pt.control_status_trb(pcs, false, false, tag, CRG_INT_TARGET, USB_RECV);
|
||||
let (_enq_pt, _pcs) = this.udc_ep[0].increment_enq_pt();
|
||||
this.knock_doorbell(0);
|
||||
println!("ep0 sent");
|
||||
}
|
||||
_ => {
|
||||
println!("A request was not handled {:x}", request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret = CrgEvent::Data(0, 0, 1);
|
||||
}
|
||||
TrbType::DataStage => {
|
||||
panic!("data stage needs handling");
|
||||
}
|
||||
_ => {
|
||||
println!("Unexpected trb_type {:?}", event_trb.get_trb_type());
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
||||
// This "belongs" in the bao-usb1x crate except that crate::println! is mapped to the right port
|
||||
// when it is in *this* crate
|
||||
pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> bool {
|
||||
let mut reset = false;
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("handle_event: {:x?}", event_trb);
|
||||
let pei = event_trb.get_endpoint_id();
|
||||
|
|
@ -3049,12 +2817,11 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
crate::println!("{:?}", portsc);
|
||||
|
||||
if portsc.prc() && !portsc.pr() {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("update_current_speed() - reset done");
|
||||
this.update_current_speed();
|
||||
reset = true;
|
||||
}
|
||||
if portsc.csc() && portsc.ppc() && portsc.pp() && portsc.ccs() {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("update_current_speed() - cable connect");
|
||||
this.update_current_speed();
|
||||
}
|
||||
|
|
@ -3063,10 +2830,9 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
TrbType::EventTransfer => {
|
||||
let comp_code =
|
||||
CompletionCode::try_from(event_trb.dw2.compl_code()).expect("Invalid completion code");
|
||||
let residual_length = event_trb.dw2.trb_tran_len() as u16;
|
||||
|
||||
// update the dequeue pointer
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("event_transfer {:x?}", event_trb);
|
||||
let deq_pt =
|
||||
unsafe { (event_trb.dw0 as *mut TransferTrbS).add(1).as_mut().expect("Couldn't deref ptr") };
|
||||
if deq_pt.get_trb_type() == TrbType::Link {
|
||||
|
|
@ -3079,13 +2845,14 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
|
||||
let dir = (pei & 1) != 0;
|
||||
if pei == 0 {
|
||||
if comp_code == CompletionCode::Success {
|
||||
if comp_code == CompletionCode::Success || comp_code == CompletionCode::SetupTagMismatchError
|
||||
{
|
||||
// ep0_xfer_complete
|
||||
if dir == USB_SEND {
|
||||
// (out, in_complete, setup)
|
||||
ret = CrgEvent::Data(0, 1, 0); // FIXME: this ordering contradicts the `dir` bit, but seems necessary to trigger the next packet send
|
||||
ret = CrgEvent::Data(0, 1, 0);
|
||||
} else {
|
||||
ret = CrgEvent::Data(1, 0, 0); // FIXME: this ordering contradicts the `dir` bit, but seems necessary to trigger the next packet send
|
||||
ret = CrgEvent::Data(1, 0, 0);
|
||||
}
|
||||
} else {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
|
|
@ -3096,6 +2863,7 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
if comp_code == CompletionCode::Success || comp_code == CompletionCode::ShortPacket {
|
||||
let ep = pei as u16 / 2;
|
||||
let ep_onehot = 1u16 << ep;
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(
|
||||
"EP{}[{:x}] xfer event, dir {}",
|
||||
ep,
|
||||
|
|
@ -3109,7 +2877,7 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
let p_trb = unsafe { &*(event_trb.dw0 as *const TransferTrbS) };
|
||||
this.app_ptr = Some(AppPtr {
|
||||
addr: p_trb.dplo as usize,
|
||||
len: (p_trb.dw2.0 & 0xffff) as usize,
|
||||
len: (p_trb.dw2.0 & 0xffff) as usize - residual_length as usize,
|
||||
ep: ep as u8,
|
||||
});
|
||||
if dir {
|
||||
|
|
@ -3120,21 +2888,19 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
ret = CrgEvent::Data(0, ep_onehot, 0)
|
||||
}
|
||||
} else if comp_code == CompletionCode::MissedServiceError {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("MissedServiceError");
|
||||
} else {
|
||||
#[cfg(feature = "std")]
|
||||
crate::println!("EventTransfer {:?} event not handled", comp_code);
|
||||
}
|
||||
}
|
||||
}
|
||||
TrbType::SetupPkt => {
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(" handle_setup_pkt");
|
||||
let mut setup_storage = [0u8; 8];
|
||||
setup_storage.copy_from_slice(&event_trb.get_raw_setup());
|
||||
this.setup = Some(setup_storage);
|
||||
this.setup_tag = event_trb.get_setup_tag();
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!(" **handle_setup_pkt tag {}", this.setup_tag);
|
||||
ret = CrgEvent::Data(0, 0, 1);
|
||||
}
|
||||
TrbType::DataStage => {
|
||||
|
|
@ -3146,5 +2912,6 @@ pub fn handle_event_inner(this: &mut CorigineUsb, event_trb: &mut EventTrbS) {
|
|||
}
|
||||
#[cfg(feature = "verbose-debug")]
|
||||
crate::println!("handle_event_inner: {:?}", ret);
|
||||
this.event_inner = Some(ret)
|
||||
this.event_inner = Some(ret);
|
||||
reset
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue