-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
144 lines (133 loc) · 4.21 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#![no_std]
use core::convert::TryFrom;
use usb_device::class_prelude::{
DescriptorWriter, EndpointIn, EndpointOut, InterfaceNumber, UsbBus, UsbBusAllocator, UsbClass,
UsbError,
};
pub struct MidiClass<'a, B>
where
B: UsbBus,
{
audio_control_if: InterfaceNumber,
midi_streaming_if: InterfaceNumber,
out_ep: EndpointOut<'a, B>,
in_ep: EndpointIn<'a, B>,
}
impl<'a, B> MidiClass<'a, B>
where
B: UsbBus,
{
pub fn new(alloc: &'a UsbBusAllocator<B>) -> Self {
Self {
audio_control_if: alloc.interface(),
midi_streaming_if: alloc.interface(),
out_ep: alloc.bulk(64),
in_ep: alloc.bulk(64),
}
}
pub fn read(&self, buf: &mut [u8]) -> Result<usize, UsbError> {
self.out_ep.read(buf)
}
pub fn write(&self, buf: &[u8]) -> Result<usize, UsbError> {
self.in_ep.write(buf)
}
}
impl<'a, B> UsbClass<B> for MidiClass<'a, B>
where
B: UsbBus,
{
fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<(), UsbError> {
writer.interface(
self.audio_control_if,
0x01, // Audio class
0x01, // Audio control subclass
0x00, // Protocol (Unused)
)?;
// Class-specific AC interface descriptor
let cs_ac_size_handle = writer.defer_ahead(5..7);
let cs_ac_start = writer.position();
writer.write(
0x24, // CS_INTERFACE
&[
0x01, // HEADER subtype
0x00,
0x01, // Audio device class specification revision 1.0
0x00,
0x00, // Total size of class specific descriptors
0x01, // Number of streaming interfaces belonging to this control iface:
self.midi_streaming_if.into(),
],
)?;
let cs_ac_end = writer.position();
let cs_ac_size = u16::try_from(cs_ac_end - cs_ac_start).unwrap();
writer
.get_deferred_mut(&cs_ac_size_handle)?
.copy_from_slice(&cs_ac_size.to_le_bytes());
writer.interface(
self.midi_streaming_if,
0x01, // Audio class
0x03, // Midi streaming subclass
0x00, // Protocol (Unused)
)?;
// Class-specific MS interface descriptor
let cs_ms_size_handle = writer.defer_ahead(5..7);
let cs_ms_start = writer.position();
writer.write(
0x24, // CS_INTERFACE
&[
0x01, // HEADER subtype
0x00, 0x01, // Midi device class specification revision 1.0
0x00, 0x00, // Total size of class specific descriptors
],
)?;
// MIDI IN Jack Descriptor
writer.write(
0x24, // CS_INTERFACE
&[
0x02, // MIDI_IN_JACK subtype
0x01, // EMBEDDED
0x01, // Jack ID
0x00, // Unused
],
)?;
// MIDI OUT Jack descriptor
writer.write(
0x24, // CS_INTERFACE
&[
0x03, // MIDI_OUT_JACK subtype
0x01, // EMBEDDED
0x02, // Jack ID
0x01, // Number of input pins:
0x01, // Entity ID [1]
0x01, // Entity output pin number [1]
0x00, // Unused
],
)?;
writer.endpoint(&self.out_ep)?;
// Class-specific
writer.write(
0x25, // CS_ENDPOINT
&[
0x01, // MS_GENERAL subtype
0x01, // Number of embedded MIDI IN jacks:
0x01, // Jack ID [1]
],
)?;
writer.endpoint(&self.in_ep)?;
// Class-specific
writer.write(
0x25, // CS_ENDPOINT
&[
0x01, // MS_GENERAL subtype
0x01, // Number of embedded MIDI OUT jacks:
0x02, // Jack ID [1]
],
)?;
let cs_ms_end = writer.position();
let cs_ms_size = u16::try_from(cs_ms_end - cs_ms_start).unwrap();
writer
.get_deferred_mut(&cs_ms_size_handle)?
.copy_from_slice(&cs_ms_size.to_le_bytes());
Ok(())
}
}