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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! `smolusb` device types
//!
use crate::descriptor::microsoft10;
use crate::descriptor::{
ConfigurationDescriptor, DescriptorType, DeviceDescriptor, DeviceQualifierDescriptor,
StringDescriptor, StringDescriptorNumber, StringDescriptorZero,
};
use crate::setup::SetupPacket;
use crate::traits::{AsByteSliceIterator, UsbDriver};
use log::{debug, trace, warn};
/// The set of descriptors describing a USB device.
pub struct Descriptors<'a> {
// required
pub device_speed: Speed,
pub device_descriptor: DeviceDescriptor,
pub configuration_descriptor: ConfigurationDescriptor<'a>,
pub string_descriptor_zero: StringDescriptorZero<'a>,
pub string_descriptors: &'a [&'a StringDescriptor<'a>],
// optional
pub device_qualifier_descriptor: Option<DeviceQualifierDescriptor>,
pub other_speed_configuration_descriptor: Option<ConfigurationDescriptor<'a>>,
pub microsoft10: Option<microsoft10::Descriptors<'a>>,
}
impl Descriptors<'_> {
/// Calculates the total length of the descriptor and returns an updated instance.
///
/// TODO ugly hack because I haven't figured out how to do this at compile time yet
#[must_use]
pub fn set_total_lengths(mut self) -> Self {
self.configuration_descriptor.set_total_length();
if let Some(other_speed_configuration_descriptor) =
self.other_speed_configuration_descriptor.as_mut()
{
other_speed_configuration_descriptor.set_total_length();
}
self
}
/// Writes the descriptor corresponding to the request.
///
/// Returns the given [`SetupPacket`] if the descriptor request could not be handled.
#[allow(clippy::too_many_lines)] // ...and sometimes clippy has opinions it should keep to itself!
pub fn write<D>(
&self,
usb: &D,
endpoint_number: u8,
setup_packet: SetupPacket,
) -> Option<SetupPacket>
where
D: UsbDriver,
{
// extract the descriptor type and number from our SETUP request
let [descriptor_number, descriptor_type_bits] = setup_packet.value.to_le_bytes();
let descriptor_type = DescriptorType::from(descriptor_type_bits);
// if the host is requesting less than the maximum amount of data,
// only respond with the amount requested
let requested_length = setup_packet.length as usize;
let bytes_written = match (&descriptor_type, descriptor_number) {
(DescriptorType::Device, 0) => usb.write_requested(
endpoint_number,
requested_length,
self.device_descriptor
.as_iter()
.copied()
.take(requested_length),
),
(DescriptorType::Configuration, _) => usb.write_requested(
endpoint_number,
requested_length,
self.configuration_descriptor
.iter()
.copied()
.take(requested_length),
),
(DescriptorType::DeviceQualifier, _) => {
if self.device_speed == Speed::High {
if let Some(descriptor) = &self.device_qualifier_descriptor {
usb.write_requested(
endpoint_number,
requested_length,
descriptor.as_iter().copied().take(requested_length),
)
} else {
// no device qualifier configured, ack HostToDevice instead - TODO check check on mac/windows
debug!(" No device qualifier configured for high-speed device");
usb.write(endpoint_number, [].into_iter())
}
} else {
// for full/low speed devices, ack HostToDevice instead - TODO check on mac/windows
trace!(
" Device qualifier request is not supported for full/low-speed devices"
);
// FIXME we should stall instead
usb.write(endpoint_number, [].into_iter())
}
}
(DescriptorType::OtherSpeedConfiguration, _) => {
if let Some(descriptor) = self.other_speed_configuration_descriptor {
usb.write_requested(
endpoint_number,
requested_length,
descriptor.iter().copied().take(requested_length),
)
} else {
// no other speed configuration, ack HostToDevice instead - TODO check check on mac/windows
debug!(" Descriptors::write_descriptor() - no other speed configuration descriptor configured");
// FIXME we should stall instead
usb.write(endpoint_number, [].into_iter())
}
}
(DescriptorType::String, StringDescriptorNumber::Zero) => usb.write_requested(
endpoint_number,
requested_length,
self.string_descriptor_zero
.iter()
.copied()
.take(requested_length),
),
(DescriptorType::String, StringDescriptorNumber::Microsoft) => {
match &self.microsoft10 {
Some(descriptors) => usb.write_requested(
endpoint_number,
requested_length,
descriptors.string_descriptor.iter(),
),
_ => {
warn!(
"Descriptors::write_descriptor() - no ms os 1.0 string descriptor defined",
);
usb.stall_endpoint_in(endpoint_number);
return Some(setup_packet);
}
}
}
(DescriptorType::String, number) => {
let offset_index: usize = (number - 1).into();
if offset_index > self.string_descriptors.len() {
warn!(
"Descriptors::write_descriptor() - unknown string descriptor {}",
number
);
return Some(setup_packet);
}
usb.write_requested(
endpoint_number,
requested_length,
self.string_descriptors[offset_index]
.iter()
.take(requested_length),
)
}
_ => {
warn!(
" Descriptors::write_descriptor() - unhandled descriptor request {:?}, {}",
descriptor_type, descriptor_number
);
return Some(setup_packet);
}
};
trace!(" wrote {} byte descriptor", bytes_written);
// consumed
None
}
}
/// USB device speed
///
/// Note: These match UTMI's `xcvr_select` constant so the mapping may not be correct for other contexts.
/// See: <https://github.com/greatscottgadgets/luna/blob/main/luna/gateware/usb/usb2/__init__.py>
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum Speed {
/// High speed (480 Mbps)
High = 0,
/// Full speed (12 Mbps)
Full = 1,
/// Low speed (1.5 Mbps)
Low = 2,
/// Super Speed (5/10 Gbps - includes `SuperSpeed+`)
Super = 3,
/// unsupported: compatibility fallback
SuperPlus = 4,
/// unsupported: compatibility fallback
Unknown = 0xff,
}
impl From<u8> for Speed {
fn from(value: u8) -> Self {
match value & 0b11 {
0 => Speed::High, // gateware gives 1
1 => Speed::Full,
2 => Speed::Low,
3 => Speed::Super,
_ => unimplemented!(),
}
}
}
impl Speed {
/// Convert from a libusb speed constant to smolusb
///
/// See: <https://github.com/libusb/libusb/blob/6bf2db6feaf3b611c9adedb6c4962a07f5cb07ae/libusb/libusb.h#L1126>
#[must_use]
pub fn from_libusb(value: u8) -> Self {
match value {
1 => Speed::Low,
2 => Speed::Full,
3 => Speed::High,
4 | 5 => Speed::Super,
_ => Speed::Unknown,
}
}
#[must_use]
pub fn to_libusb(&self) -> u8 {
match self {
Speed::Low => 1,
Speed::Full => 2,
Speed::High => 3,
Speed::Super => 4,
_ => 0,
}
}
}