fix scalar return discipline in usb-bao1x

also add ability to read keyboard led state
This commit is contained in:
bunnie 2025-11-24 18:54:50 +08:00
parent 5964328bcf
commit 543f455ad0
3 changed files with 139 additions and 114 deletions

View file

@ -17,6 +17,7 @@ use xous_usb_hid::device::DeviceClass;
use xous_usb_hid::device::fido::RawFido; use xous_usb_hid::device::fido::RawFido;
use xous_usb_hid::device::fido::RawFidoConfig; use xous_usb_hid::device::fido::RawFidoConfig;
use xous_usb_hid::device::fido::RawFidoReport; use xous_usb_hid::device::fido::RawFidoReport;
use xous_usb_hid::device::keyboard::KeyboardLedsReport;
use xous_usb_hid::device::keyboard::{NKROBootKeyboard, NKROBootKeyboardConfig}; use xous_usb_hid::device::keyboard::{NKROBootKeyboard, NKROBootKeyboardConfig};
use xous_usb_hid::page::Keyboard; use xous_usb_hid::page::Keyboard;
use xous_usb_hid::prelude::UsbHidClass; use xous_usb_hid::prelude::UsbHidClass;
@ -55,6 +56,7 @@ pub struct Bao1xUsb<'a> {
// used for debugging, the idea is to query this in userspace to try and pick up the double-lock problem // used for debugging, the idea is to query this in userspace to try and pick up the double-lock problem
// from the interrupt handler. // from the interrupt handler.
pub double_lock: AtomicBool, pub double_lock: AtomicBool,
pub led_state: KeyboardLedsReport,
} }
impl<'a> Bao1xUsb<'a> { impl<'a> Bao1xUsb<'a> {
@ -117,6 +119,7 @@ impl<'a> Bao1xUsb<'a> {
serial_port, serial_port,
serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE], serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE],
double_lock: AtomicBool::new(false), double_lock: AtomicBool::new(false),
led_state: KeyboardLedsReport::default(),
} }
} }
@ -280,9 +283,7 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) {
} }
match class.device::<NKROBootKeyboard<_>, _>().read_report() { match class.device::<NKROBootKeyboard<_>, _>().read_report() {
Ok(l) => { Ok(l) => {
// for now all we do is just print this, we don't usb.led_state = l;
// actually store the data or pass it on to userspace
crate::println!("keyboard LEDs: {:?}", l);
} }
Err(e) => match e { Err(e) => match e {
UsbError::WouldBlock => {} UsbError::WouldBlock => {}

View file

@ -92,7 +92,9 @@ impl UsbHid {
self.conn, self.conn,
Message::new_blocking_scalar(Opcode::LinkStatus.to_usize().unwrap(), 0, 0, 0, 0), Message::new_blocking_scalar(Opcode::LinkStatus.to_usize().unwrap(), 0, 0, 0, 0),
) { ) {
Ok(xous::Result::Scalar1(code)) => match code { // this is manually unpacked because the usb_device crate doesn't have a From/To implementation
// for UsbDeviceState :-/
Ok(xous::Result::Scalar5(_, code, _, _, _)) => match code {
0 => UsbDeviceState::Default, 0 => UsbDeviceState::Default,
1 => UsbDeviceState::Addressed, 1 => UsbDeviceState::Addressed,
2 => UsbDeviceState::Configured, 2 => UsbDeviceState::Configured,
@ -120,7 +122,7 @@ impl UsbHid {
if auto_keyup { 1 } else { 0 }, if auto_keyup { 1 } else { 0 },
), ),
) { ) {
Ok(xous::Result::Scalar1(code)) => { Ok(xous::Result::Scalar5(_, code, _, _, _)) => {
match code { match code {
0 => Ok(()), 0 => Ok(()),
// indicates that we aren't connected to a host to send characters // indicates that we aren't connected to a host to send characters
@ -159,7 +161,7 @@ impl UsbHid {
self.conn, self.conn,
Message::new_blocking_scalar(Opcode::GetLedState.to_usize().unwrap(), 0, 0, 0, 0), Message::new_blocking_scalar(Opcode::GetLedState.to_usize().unwrap(), 0, 0, 0, 0),
) { ) {
Ok(xous::Result::Scalar1(code)) => match KeyboardLedsReport::unpack(&[code as u8]) { Ok(xous::Result::Scalar5(_, code, _, _, _)) => match KeyboardLedsReport::unpack(&[code as u8]) {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(_) => Err(xous::Error::InternalError), Err(_) => Err(xous::Error::InternalError),
}, },

View file

@ -23,9 +23,10 @@ use bao1x_hal::usb::driver::{CorigineUsb, CorigineWrapper};
use hw::Bao1xUsb; use hw::Bao1xUsb;
use hw::UsbIrqReq; use hw::UsbIrqReq;
use num_traits::*; use num_traits::*;
use packed_struct::PackedStructSlice;
use usb_device::class_prelude::*; use usb_device::class_prelude::*;
use utralib::{AtomicCsr, utra}; use utralib::{AtomicCsr, utra};
use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; use xous::msg_scalar_unpack;
use xous_ipc::Buffer; use xous_ipc::Buffer;
use xous_usb_hid::device::fido::RawFidoReport; use xous_usb_hid::device::fido::RawFidoReport;
use xous_usb_hid::page::Keyboard; use xous_usb_hid::page::Keyboard;
@ -443,7 +444,12 @@ pub(crate) fn main_hw() -> ! {
} }
buffer.replace(u2f_ipc).unwrap(); buffer.replace(u2f_ipc).unwrap();
} }
Opcode::SendKeyCode => msg_blocking_scalar_unpack!(msg, code0, code1, code2, autoup, { Opcode::SendKeyCode => {
if let Some(scalar) = msg.body.scalar_message_mut() {
let code0 = scalar.arg1;
let code1 = scalar.arg2;
let code2 = scalar.arg3;
let autoup = scalar.arg4;
let native_map = native_kbd.get_keymap().unwrap(); let native_map = native_kbd.get_keymap().unwrap();
if code0 != 0 { if code0 != 0 {
cu.kbd_tx_queue.borrow_mut().push_back(match native_map { cu.kbd_tx_queue.borrow_mut().push_back(match native_map {
@ -475,9 +481,9 @@ pub(crate) fn main_hw() -> ! {
cu.sw_irq(UsbIrqReq::KbdTx); cu.sw_irq(UsbIrqReq::KbdTx);
tt.sleep_ms(autotype_delay_ms).ok(); tt.sleep_ms(autotype_delay_ms).ok();
} }
xous::return_scalar(msg.sender, 0).unwrap(); scalar.arg1 = 0;
}), }
}
Opcode::SendString => { Opcode::SendString => {
let mut buffer = let mut buffer =
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
@ -546,7 +552,9 @@ pub(crate) fn main_hw() -> ! {
LogLevel::Err => log::set_max_level(log::LevelFilter::Error), LogLevel::Err => log::set_max_level(log::LevelFilter::Error),
} }
}), }),
Opcode::IrqSerialRx => msg_scalar_unpack!(msg, valid_bytes, _, _, _, { Opcode::IrqSerialRx => {
if let Some(scalar) = msg.body.scalar_message() {
let valid_bytes = scalar.arg1;
serial_buf.extend_from_slice(&cu.serial_rx[..valid_bytes]); serial_buf.extend_from_slice(&cu.serial_rx[..valid_bytes]);
match serial_listen_mode { match serial_listen_mode {
SerialListenMode::NoListener => { SerialListenMode::NoListener => {
@ -607,7 +615,9 @@ pub(crate) fn main_hw() -> ! {
match serial_listener.take() { match serial_listener.take() {
Some(mut rx_msg) => { Some(mut rx_msg) => {
let mut response = unsafe { let mut response = unsafe {
Buffer::from_memory_message_mut(rx_msg.body.memory_message_mut().unwrap()) Buffer::from_memory_message_mut(
rx_msg.body.memory_message_mut().unwrap(),
)
}; };
let mut buf = response.to_original::<UsbSerialBinary, _>().unwrap(); let mut buf = response.to_original::<UsbSerialBinary, _>().unwrap();
let n = serial_buf.len().min(SERIAL_BINARY_BUFLEN); let n = serial_buf.len().min(SERIAL_BINARY_BUFLEN);
@ -622,7 +632,8 @@ pub(crate) fn main_hw() -> ! {
} }
} }
} }
}), }
}
Opcode::SerialHookAscii => { Opcode::SerialHookAscii => {
let maybe_delimiter = { let maybe_delimiter = {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -740,8 +751,7 @@ pub(crate) fn main_hw() -> ! {
// the logger API is "best effort" only. Because retries and response codes can cause problems // the logger API is "best effort" only. Because retries and response codes can cause problems
// in the logger API, if anything goes wrong, we prefer to discard characters rather than get // in the logger API, if anything goes wrong, we prefer to discard characters rather than get
// the whole subsystem stuck in some awful recursive error handling hell. // the whole subsystem stuck in some awful recursive error handling hell.
match msg.body.memory_message() { if let Some(mem_msg) = msg.body.memory_message() {
Some(mem_msg) => {
let buffer = unsafe { Buffer::from_memory_message(mem_msg) }; let buffer = unsafe { Buffer::from_memory_message(mem_msg) };
match buffer.to_original::<api::UsbString, _>() { match buffer.to_original::<api::UsbString, _>() {
Ok(usb_send) => { Ok(usb_send) => {
@ -754,7 +764,19 @@ pub(crate) fn main_hw() -> ! {
_ => {} // silent errors _ => {} // silent errors
} }
} }
_ => {} // silent errors }
Opcode::LinkStatus => {
if let Some(scalar) = msg.body.scalar_message_mut() {
// to get the raw device state:
// cu.device.bus().core().get_device_state()
scalar.arg1 = cu.device.state() as usize;
}
}
Opcode::GetLedState => {
if let Some(scalar) = msg.body.scalar_message_mut() {
let mut code = [0u8; 1];
cu.led_state.pack_to_slice(&mut code).unwrap();
scalar.arg1 = code[0] as usize;
} }
} }
Opcode::InvalidCall => { Opcode::InvalidCall => {
@ -766,7 +788,7 @@ pub(crate) fn main_hw() -> ! {
} }
_ => { _ => {
unimplemented!( unimplemented!(
"Opcode {:?} not implemented for this version of the stack: {:?}", "Opcode {:?} not implemented for this version of the USB stack: {:?}",
opcode, opcode,
msg msg
); );