usb: set the interface handler in InterfaceBuilder.

This commit is contained in:
Dario Nieuwenhuis 2022-04-23 01:29:19 +02:00
parent 092c2b7dfe
commit 7c6a88f3dd
3 changed files with 12 additions and 9 deletions

View File

@ -112,7 +112,8 @@ fn build<'d, D: Driver<'d>>(
let len = config.report_descriptor.len(); let len = config.report_descriptor.len();
let mut func = builder.function(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); let mut func = builder.function(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE);
let mut iface = func.interface(Some(control)); let mut iface = func.interface();
iface.handler(control);
let mut alt = iface.alt_setting(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE); let mut alt = iface.alt_setting(USB_CLASS_HID, USB_SUBCLASS_NONE, USB_PROTOCOL_NONE);
// HID descriptor // HID descriptor
@ -438,7 +439,7 @@ impl<'d> ControlHandler for Control<'d> {
self.out_report_offset.store(0, Ordering::Release); self.out_report_offset.store(0, Ordering::Release);
} }
fn get_descriptor<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> { fn get_descriptor<'a>(&'a mut self, req: Request, _buf: &'a mut [u8]) -> InResponse<'a> {
match (req.value >> 8) as u8 { match (req.value >> 8) as u8 {
HID_DESC_DESCTYPE_HID_REPORT => InResponse::Accepted(self.report_descriptor), HID_DESC_DESCTYPE_HID_REPORT => InResponse::Accepted(self.report_descriptor),
HID_DESC_DESCTYPE_HID => InResponse::Accepted(&self.hid_descriptor), HID_DESC_DESCTYPE_HID => InResponse::Accepted(&self.hid_descriptor),

View File

@ -178,7 +178,8 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
let mut func = builder.function(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); let mut func = builder.function(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE);
// Control interface // Control interface
let mut iface = func.interface(Some(control)); let mut iface = func.interface();
iface.handler(control);
let comm_if = iface.interface_number(); let comm_if = iface.interface_number();
let data_if = u8::from(comm_if) + 1; let data_if = u8::from(comm_if) + 1;
let mut alt = iface.alt_setting(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE); let mut alt = iface.alt_setting(USB_CLASS_CDC, CDC_SUBCLASS_ACM, CDC_PROTOCOL_NONE);
@ -218,7 +219,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
let comm_ep = alt.endpoint_interrupt_in(8, 255); let comm_ep = alt.endpoint_interrupt_in(8, 255);
// Data interface // Data interface
let mut iface = func.interface(None); let mut iface = func.interface();
let data_if = iface.interface_number(); let data_if = iface.interface_number();
let mut alt = iface.alt_setting(USB_CLASS_CDC_DATA, 0x00, CDC_PROTOCOL_NONE); let mut alt = iface.alt_setting(USB_CLASS_CDC_DATA, 0x00, CDC_PROTOCOL_NONE);
let read_ep = alt.endpoint_bulk_out(max_packet_size); let read_ep = alt.endpoint_bulk_out(max_packet_size);

View File

@ -267,17 +267,14 @@ impl<'a, 'd, D: Driver<'d>> FunctionBuilder<'a, 'd, D> {
/// Add an interface to the function. /// Add an interface to the function.
/// ///
/// Interface numbers are guaranteed to be allocated consecutively, starting from 0. /// Interface numbers are guaranteed to be allocated consecutively, starting from 0.
pub fn interface( pub fn interface(&mut self) -> InterfaceBuilder<'_, 'd, D> {
&mut self,
handler: Option<&'d mut dyn ControlHandler>,
) -> InterfaceBuilder<'_, 'd, D> {
if let Some(i) = self.iface_count_index { if let Some(i) = self.iface_count_index {
self.builder.config_descriptor.buf[i] += 1; self.builder.config_descriptor.buf[i] += 1;
} }
let number = self.builder.interfaces.len() as _; let number = self.builder.interfaces.len() as _;
let iface = Interface { let iface = Interface {
handler, handler: None,
current_alt_setting: 0, current_alt_setting: 0,
num_alt_settings: 0, num_alt_settings: 0,
}; };
@ -307,6 +304,10 @@ impl<'a, 'd, D: Driver<'d>> InterfaceBuilder<'a, 'd, D> {
self.interface_number self.interface_number
} }
pub fn handler(&mut self, handler: &'d mut dyn ControlHandler) {
self.builder.interfaces[self.interface_number.0 as usize].handler = Some(handler);
}
/// Add an alternate setting to the interface and write its descriptor. /// Add an alternate setting to the interface and write its descriptor.
/// ///
/// Alternate setting numbers are guaranteed to be allocated consecutively, starting from 0. /// Alternate setting numbers are guaranteed to be allocated consecutively, starting from 0.