mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
services/usb-device-xous: format code
This commit is contained in:
parent
a64edbdf2f
commit
73d3563cb6
3 changed files with 53 additions and 78 deletions
|
|
@ -171,9 +171,7 @@ pub struct HIDReportDescriptorMessage {
|
||||||
pub struct HIDReport(pub [u8; 64]);
|
pub struct HIDReport(pub [u8; 64]);
|
||||||
|
|
||||||
impl Default for HIDReport {
|
impl Default for HIDReport {
|
||||||
fn default() -> Self {
|
fn default() -> Self { return Self([0u8; 64]) }
|
||||||
return Self([0u8; 64])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, Default, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::api::HIDReport;
|
|
||||||
use frunk_core::hlist::{HCons, HNil};
|
|
||||||
use fugit::ExtU32;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{collections::VecDeque, sync::Mutex};
|
use std::{collections::VecDeque, sync::Mutex};
|
||||||
|
|
||||||
|
use frunk_core::hlist::{HCons, HNil};
|
||||||
|
use fugit::ExtU32;
|
||||||
use usb_device::device::UsbDeviceBuilder;
|
use usb_device::device::UsbDeviceBuilder;
|
||||||
use usb_device::{
|
use usb_device::{
|
||||||
class_prelude::{UsbBus, UsbBusAllocator},
|
class_prelude::{UsbBus, UsbBusAllocator},
|
||||||
|
|
@ -13,13 +13,15 @@ use usb_device_xous::UsbDeviceState;
|
||||||
use xous_usb_hid::{
|
use xous_usb_hid::{
|
||||||
device::DeviceClass,
|
device::DeviceClass,
|
||||||
interface::{
|
interface::{
|
||||||
HeapInterface, HeapInterfaceBuilder, HeapInterfaceConfig, InBytes64, OutBytes64,
|
HeapInterface, HeapInterfaceBuilder, HeapInterfaceConfig, InBytes64, OutBytes64, ReportSingle,
|
||||||
ReportSingle, UsbAllocatable,
|
UsbAllocatable,
|
||||||
},
|
},
|
||||||
usb_class::{UsbHidClass, UsbHidClassBuilder},
|
usb_class::{UsbHidClass, UsbHidClassBuilder},
|
||||||
UsbHidError,
|
UsbHidError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::api::HIDReport;
|
||||||
|
|
||||||
/// AppHIDDevice implements a UsbBus device whose internal USB interface allows for a heap-allocated
|
/// AppHIDDevice implements a UsbBus device whose internal USB interface allows for a heap-allocated
|
||||||
/// device report descriptor.
|
/// device report descriptor.
|
||||||
pub struct AppHIDDevice<'a, B: UsbBus> {
|
pub struct AppHIDDevice<'a, B: UsbBus> {
|
||||||
|
|
@ -28,10 +30,7 @@ pub struct AppHIDDevice<'a, B: UsbBus> {
|
||||||
|
|
||||||
impl<'a, B: UsbBus> AppHIDDevice<'a, B> {
|
impl<'a, B: UsbBus> AppHIDDevice<'a, B> {
|
||||||
pub fn write_report(&mut self, report: &HIDReport) -> Result<(), UsbHidError> {
|
pub fn write_report(&mut self, report: &HIDReport) -> Result<(), UsbHidError> {
|
||||||
self.interface
|
self.interface.write_report(&report.0).map(|_| ()).map_err(UsbHidError::from)
|
||||||
.write_report(&report.0)
|
|
||||||
.map(|_| ())
|
|
||||||
.map_err(UsbHidError::from)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_report(&mut self) -> usb_device::Result<HIDReport> {
|
pub fn read_report(&mut self) -> usb_device::Result<HIDReport> {
|
||||||
|
|
@ -42,10 +41,7 @@ impl<'a, B: UsbBus> AppHIDDevice<'a, B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_device_descriptor(
|
pub fn set_device_descriptor(&mut self, descriptor: Vec<u8>) -> Result<(), usb_device::UsbError> {
|
||||||
&mut self,
|
|
||||||
descriptor: Vec<u8>,
|
|
||||||
) -> Result<(), usb_device::UsbError> {
|
|
||||||
self.interface.set_report_descriptor(descriptor)
|
self.interface.set_report_descriptor(descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -53,15 +49,11 @@ impl<'a, B: UsbBus> AppHIDDevice<'a, B> {
|
||||||
impl<'a, B: UsbBus> DeviceClass<'a> for AppHIDDevice<'a, B> {
|
impl<'a, B: UsbBus> DeviceClass<'a> for AppHIDDevice<'a, B> {
|
||||||
type I = HeapInterface<'a, B, InBytes64, OutBytes64, ReportSingle>;
|
type I = HeapInterface<'a, B, InBytes64, OutBytes64, ReportSingle>;
|
||||||
|
|
||||||
fn interface(&mut self) -> &mut Self::I {
|
fn interface(&mut self) -> &mut Self::I { &mut self.interface }
|
||||||
&mut self.interface
|
|
||||||
}
|
|
||||||
|
|
||||||
fn reset(&mut self) {}
|
fn reset(&mut self) {}
|
||||||
|
|
||||||
fn tick(&mut self) -> Result<(), UsbHidError> {
|
fn tick(&mut self) -> Result<(), UsbHidError> { Ok(()) }
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppHIDConfig<'a> {
|
pub struct AppHIDConfig<'a> {
|
||||||
|
|
@ -95,9 +87,7 @@ impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for AppHIDConfig<'a> {
|
||||||
type Allocated = AppHIDDevice<'a, B>;
|
type Allocated = AppHIDDevice<'a, B>;
|
||||||
|
|
||||||
fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
|
fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
|
||||||
Self::Allocated {
|
Self::Allocated { interface: HeapInterface::new(usb_alloc, self.interface) }
|
||||||
interface: HeapInterface::new(usb_alloc, self.interface),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,8 +108,8 @@ pub enum AppHIDError {
|
||||||
/// It buffers incoming HID reports in a queue until it is requested to return some.
|
/// It buffers incoming HID reports in a queue until it is requested to return some.
|
||||||
/// Developers can set the maximum amount of HID reports to buffer, and AppHID will drop the oldest when a new
|
/// Developers can set the maximum amount of HID reports to buffer, and AppHID will drop the oldest when a new
|
||||||
/// one is added, and threshold is met.
|
/// one is added, and threshold is met.
|
||||||
/// The buffer is heap-allocated: one HID report is 64 bytes, so depending on your configuration you might need
|
/// The buffer is heap-allocated: one HID report is 64 bytes, so depending on your configuration you might
|
||||||
/// to increase the heap limits for your process.
|
/// need to increase the heap limits for your process.
|
||||||
pub struct AppHID<'a, B: UsbBus> {
|
pub struct AppHID<'a, B: UsbBus> {
|
||||||
max_stored_reports: usize,
|
max_stored_reports: usize,
|
||||||
incoming_reports: VecDeque<HIDReport>,
|
incoming_reports: VecDeque<HIDReport>,
|
||||||
|
|
@ -157,14 +147,10 @@ impl<'a, B: UsbBus> AppHID<'a, B> {
|
||||||
|
|
||||||
/// Forces the reset of the underlying USB device.
|
/// Forces the reset of the underlying USB device.
|
||||||
/// Causes host to re-enumerate.
|
/// Causes host to re-enumerate.
|
||||||
pub fn force_reset(&mut self) -> usb_device::Result<()> {
|
pub fn force_reset(&mut self) -> usb_device::Result<()> { self.device.force_reset() }
|
||||||
self.device.force_reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current state of the underlying USB device.
|
/// Returns the current state of the underlying USB device.
|
||||||
pub fn state(&self) -> UsbDeviceState {
|
pub fn state(&self) -> UsbDeviceState { self.device.state() }
|
||||||
self.device.state()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Polls for new reports on the bus, sent from the USB host.
|
/// Polls for new reports on the bus, sent from the USB host.
|
||||||
pub fn poll(&mut self) -> Result<(), AppHIDError> {
|
pub fn poll(&mut self) -> Result<(), AppHIDError> {
|
||||||
|
|
@ -179,21 +165,22 @@ impl<'a, B: UsbBus> AppHID<'a, B> {
|
||||||
let hidv2_device = self.class.device::<AppHIDDevice<'_, _>, _>();
|
let hidv2_device = self.class.device::<AppHIDDevice<'_, _>, _>();
|
||||||
match hidv2_device.read_report() {
|
match hidv2_device.read_report() {
|
||||||
Ok(report) => {
|
Ok(report) => {
|
||||||
let result = 'result_scope : {
|
let result = 'result_scope: {
|
||||||
let reports_stored = self.incoming_reports.len();
|
let reports_stored = self.incoming_reports.len();
|
||||||
if reports_stored < self.max_stored_reports {
|
if reports_stored < self.max_stored_reports {
|
||||||
break 'result_scope Ok(());
|
break 'result_scope Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.incoming_reports.pop_front().unwrap();
|
self.incoming_reports.pop_front().unwrap();
|
||||||
break 'result_scope Err(AppHIDError::OldestReportDropped)
|
break 'result_scope Err(AppHIDError::OldestReportDropped);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.incoming_reports.push_back(report);
|
self.incoming_reports.push_back(report);
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// If we have something that isn't WouldBlock, maybe stuff's about to blow up: return to caller.
|
// If we have something that isn't WouldBlock, maybe stuff's about to blow up: return to
|
||||||
|
// caller.
|
||||||
if !matches!(err, UsbError::WouldBlock) {
|
if !matches!(err, UsbError::WouldBlock) {
|
||||||
return Err(AppHIDError::UsbError(err));
|
return Err(AppHIDError::UsbError(err));
|
||||||
}
|
}
|
||||||
|
|
@ -208,19 +195,16 @@ impl<'a, B: UsbBus> AppHID<'a, B> {
|
||||||
pub fn set_device_report(&mut self, descriptor: Vec<u8>) -> Result<(), AppHIDError> {
|
pub fn set_device_report(&mut self, descriptor: Vec<u8>) -> Result<(), AppHIDError> {
|
||||||
let descr_len = descriptor.len();
|
let descr_len = descriptor.len();
|
||||||
let hidv2_device = self.class.device::<AppHIDDevice<'_, _>, _>();
|
let hidv2_device = self.class.device::<AppHIDDevice<'_, _>, _>();
|
||||||
hidv2_device
|
hidv2_device.set_device_descriptor(descriptor).map_err(|e| AppHIDError::UsbError(e)).and_then(|_| {
|
||||||
.set_device_descriptor(descriptor)
|
*self.device_descr_set.lock().unwrap() = match descr_len {
|
||||||
.map_err(|e| AppHIDError::UsbError(e))
|
0 => false,
|
||||||
.and_then(|_| {
|
_ => true,
|
||||||
*self.device_descr_set.lock().unwrap() = match descr_len {
|
};
|
||||||
0 => false,
|
|
||||||
_ => true,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.device.force_reset().ok();
|
self.device.force_reset().ok();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resets the stored device descriptor report and drops all the stored incoming reports, if any.
|
/// Resets the stored device descriptor report and drops all the stored incoming reports, if any.
|
||||||
|
|
@ -231,9 +215,7 @@ impl<'a, B: UsbBus> AppHID<'a, B> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if there is a device descriptor set for the AppHID.
|
/// Returns true if there is a device descriptor set for the AppHID.
|
||||||
pub fn descriptor_set(&self) -> bool {
|
pub fn descriptor_set(&self) -> bool { *self.device_descr_set.lock().unwrap() }
|
||||||
*self.device_descr_set.lock().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the oldest HID report read from the USB bus.
|
/// Returns the oldest HID report read from the USB bus.
|
||||||
pub fn read_report(&mut self) -> Option<HIDReport> {
|
pub fn read_report(&mut self) -> Option<HIDReport> {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
use crate::*;
|
|
||||||
use crate::hid::AppHIDConfig;
|
|
||||||
use core::num::NonZeroU8;
|
use core::num::NonZeroU8;
|
||||||
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
@ -27,6 +25,9 @@ use xous_usb_hid::device::DeviceClass;
|
||||||
use xous_usb_hid::page::Keyboard;
|
use xous_usb_hid::page::Keyboard;
|
||||||
use xous_usb_hid::prelude::*;
|
use xous_usb_hid::prelude::*;
|
||||||
|
|
||||||
|
use crate::hid::AppHIDConfig;
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
/// Time allowed for switchover between device core types. It's longer because some hosts
|
/// Time allowed for switchover between device core types. It's longer because some hosts
|
||||||
/// get really confused when you have the same VID/PID show up with a different set of endpoints.
|
/// get really confused when you have the same VID/PID show up with a different set of endpoints.
|
||||||
const EXTENDED_CORE_RESET_MS: usize = 4000;
|
const EXTENDED_CORE_RESET_MS: usize = 4000;
|
||||||
|
|
@ -96,7 +97,7 @@ pub(crate) fn main_hw() -> ! {
|
||||||
if soc_ver < minimum_ver {
|
if soc_ver < minimum_ver {
|
||||||
if soc_ver.min != 0 {
|
if soc_ver.min != 0 {
|
||||||
// don't show during hosted mode, which reports 0.0.0+0
|
// don't show during hosted mode, which reports 0.0.0+0
|
||||||
tt.sleep_ms(1500).ok(); // wait for some system boot to happen before popping up the modal
|
tt.sleep_ms(1500).ok(); // wait for some system boot to happen before popping up the modal
|
||||||
let modals = modals::Modals::new(&xns).unwrap();
|
let modals = modals::Modals::new(&xns).unwrap();
|
||||||
modals.show_notification(
|
modals.show_notification(
|
||||||
&format!("SoC version >= 0.9.8+20 required for USB HID. Detected rev: {}. Refusing to start USB driver.",
|
&format!("SoC version >= 0.9.8+20 required for USB HID. Detected rev: {}. Refusing to start USB driver.",
|
||||||
|
|
@ -294,7 +295,6 @@ pub(crate) fn main_hw() -> ! {
|
||||||
100, // 100 * 64 bytes = 6.4kb, quite the backlog
|
100, // 100 * 64 bytes = 6.4kb, quite the backlog
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default();
|
let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default();
|
||||||
let mut fido_listener: Option<xous::MessageEnvelope> = None;
|
let mut fido_listener: Option<xous::MessageEnvelope> = None;
|
||||||
// under the theory that PIDs cannot be forged. TODO: check that PIDs cannot be forged.
|
// under the theory that PIDs cannot be forged. TODO: check that PIDs cannot be forged.
|
||||||
|
|
@ -935,7 +935,7 @@ pub(crate) fn main_hw() -> ! {
|
||||||
usbmgmt.ll_reset(false);
|
usbmgmt.ll_reset(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
UsbDeviceType::HIDv2 => {
|
UsbDeviceType::HIDv2 => {
|
||||||
log::info!("Connectiing HIDv2 device");
|
log::info!("Connectiing HIDv2 device");
|
||||||
match view {
|
match view {
|
||||||
|
|
@ -1103,7 +1103,9 @@ pub(crate) fn main_hw() -> ! {
|
||||||
Views::Serial => {
|
Views::Serial => {
|
||||||
xous::return_scalar(msg.sender, UsbDeviceType::Serial as usize).unwrap()
|
xous::return_scalar(msg.sender, UsbDeviceType::Serial as usize).unwrap()
|
||||||
}
|
}
|
||||||
Views::HIDv2 => xous::return_scalar(msg.sender, UsbDeviceType::HIDv2 as usize).unwrap(),
|
Views::HIDv2 => {
|
||||||
|
xous::return_scalar(msg.sender, UsbDeviceType::HIDv2 as usize).unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
xous::return_scalar(msg.sender, UsbDeviceType::Debug as usize).unwrap();
|
xous::return_scalar(msg.sender, UsbDeviceType::Debug as usize).unwrap();
|
||||||
|
|
@ -1612,9 +1614,8 @@ pub(crate) fn main_hw() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Opcode::HIDSetDescriptor) => {
|
Some(Opcode::HIDSetDescriptor) => {
|
||||||
let buffer = unsafe {
|
let buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
let data = buffer.to_original::<HIDReportDescriptorMessage, _>().unwrap();
|
let data = buffer.to_original::<HIDReportDescriptorMessage, _>().unwrap();
|
||||||
|
|
||||||
// This branch can only error if data.descriptor is longer than the
|
// This branch can only error if data.descriptor is longer than the
|
||||||
|
|
@ -1624,47 +1625,41 @@ pub(crate) fn main_hw() -> ! {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log::error!("cannot set hidv2 device report: {:?}", error);
|
log::error!("cannot set hidv2 device report: {:?}", error);
|
||||||
},
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(Opcode::HIDUnsetDescriptor) => {
|
|
||||||
match hidv2.reset_device_report() {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(error) => log::error!("cannot reset hidv2 device report: {:?}", error),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(Opcode::HIDUnsetDescriptor) => match hidv2.reset_device_report() {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(error) => log::error!("cannot reset hidv2 device report: {:?}", error),
|
||||||
|
},
|
||||||
Some(Opcode::HIDReadReport) => {
|
Some(Opcode::HIDReadReport) => {
|
||||||
if !hidv2.descriptor_set() {
|
if !hidv2.descriptor_set() {
|
||||||
log::warn!("trying to read a HID report with no descriptor set!");
|
log::warn!("trying to read a HID report with no descriptor set!");
|
||||||
continue
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buffer = unsafe {
|
let mut buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
|
|
||||||
let mut data = buffer.to_original::<HIDReportMessage, _>().unwrap();
|
let mut data = buffer.to_original::<HIDReportMessage, _>().unwrap();
|
||||||
|
|
||||||
data.data = hidv2.read_report();
|
data.data = hidv2.read_report();
|
||||||
|
|
||||||
buffer.replace(data).expect("couldn't serialize return");
|
buffer.replace(data).expect("couldn't serialize return");
|
||||||
},
|
}
|
||||||
Some(Opcode::HIDWriteReport) => {
|
Some(Opcode::HIDWriteReport) => {
|
||||||
if !hidv2.descriptor_set() {
|
if !hidv2.descriptor_set() {
|
||||||
log::warn!("trying to write a HID report with no descriptor set!");
|
log::warn!("trying to write a HID report with no descriptor set!");
|
||||||
continue
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = unsafe {
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
Buffer::from_memory_message(msg.body.memory_message().unwrap())
|
|
||||||
};
|
|
||||||
let data = buffer.to_original::<HIDReport, _>().unwrap();
|
let data = buffer.to_original::<HIDReport, _>().unwrap();
|
||||||
|
|
||||||
|
|
||||||
log::info!("report to be written to USB: {:?}", data);
|
log::info!("report to be written to USB: {:?}", data);
|
||||||
|
|
||||||
hidv2.write_report(data);
|
hidv2.write_report(data);
|
||||||
},
|
}
|
||||||
Some(Opcode::RegisterUsbObserver) => {
|
Some(Opcode::RegisterUsbObserver) => {
|
||||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
let ur = buffer.as_flat::<UsbListenerRegistration, _>().unwrap();
|
let ur = buffer.as_flat::<UsbListenerRegistration, _>().unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue