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
use crate::{Result, UsbError};
use crate::bus::{UsbBus, InterfaceNumber};
use crate::device;
use crate::endpoint::{Endpoint, EndpointDirection};
#[allow(missing_docs)]
pub mod descriptor_type {
pub const DEVICE: u8 = 1;
pub const CONFIGURATION: u8 = 2;
pub const STRING: u8 = 3;
pub const INTERFACE: u8 = 4;
pub const ENDPOINT: u8 = 5;
}
pub mod lang_id {
pub const ENGLISH_US: u16 = 0x0409;
}
pub struct DescriptorWriter<'a> {
buf: &'a mut [u8],
position: usize,
num_interfaces_mark: Option<usize>,
num_endpoints_mark: Option<usize>,
}
impl DescriptorWriter<'_> {
pub(crate) fn new(buf: &mut [u8]) -> DescriptorWriter<'_> {
DescriptorWriter {
buf,
position: 0,
num_interfaces_mark: None,
num_endpoints_mark: None,
}
}
pub fn position(&self) -> usize {
self.position
}
pub fn write(&mut self, descriptor_type: u8, descriptor: &[u8]) -> Result<()> {
let length = descriptor.len();
if self.position + 2 + length > self.buf.len() {
return Err(UsbError::BufferOverflow);
}
self.buf[self.position] = (length + 2) as u8;
self.buf[self.position + 1] = descriptor_type;
let start = self.position + 2;
self.buf[start..start + length].copy_from_slice(descriptor);
self.position = start + length;
Ok(())
}
pub(crate) fn device(&mut self, config: &device::Config) -> Result<()> {
self.write(
descriptor_type::DEVICE,
&[
0x00, 0x02,
config.device_class,
config.device_sub_class,
config.device_protocol,
config.max_packet_size_0,
config.vendor_id as u8, (config.vendor_id >> 8) as u8,
config.product_id as u8, (config.product_id >> 8) as u8,
config.device_release as u8, (config.device_release >> 8) as u8,
config.manufacturer.map_or(0, |_| 1),
config.product.map_or(0, |_| 2),
config.serial_number.map_or(0, |_| 3),
1,
])
}
pub(crate) fn configuration(&mut self, config: &device::Config) -> Result<()> {
self.num_interfaces_mark = Some(self.position + 4);
self.write(
descriptor_type::CONFIGURATION,
&[
0, 0,
0,
device::CONFIGURATION_VALUE,
0,
0x80
| if config.self_powered { 0x40 } else { 0x00 }
| if config.supports_remote_wakeup { 0x20 } else { 0x00 },
config.max_power
])
}
pub(crate) fn end_class(&mut self) {
self.num_endpoints_mark = None;
}
pub(crate) fn end_configuration(&mut self) {
let position = self.position as u16;
self.buf[2..4].copy_from_slice(&position.to_le_bytes());
}
pub fn interface(&mut self, number: InterfaceNumber,
interface_class: u8, interface_sub_class: u8, interface_protocol: u8) -> Result<()>
{
self.buf[self.num_interfaces_mark.unwrap()] += 1;
self.num_endpoints_mark = Some(self.position + 4);
self.write(
descriptor_type::INTERFACE,
&[
number.into(),
device::DEFAULT_ALTERNATE_SETTING,
0,
interface_class,
interface_sub_class,
interface_protocol,
0,
])?;
Ok(())
}
pub fn endpoint<'e, B: UsbBus, D: EndpointDirection>(&mut self, endpoint: &Endpoint<'e, B, D>)
-> Result<()>
{
self.buf[self.num_endpoints_mark.expect("missing interface descriptor")] += 1;
let mps = endpoint.max_packet_size();
self.write(
descriptor_type::ENDPOINT,
&[
endpoint.address().into(),
endpoint.ep_type() as u8,
mps as u8, (mps >> 8) as u8,
endpoint.interval(),
])?;
Ok(())
}
pub(crate) fn string(&mut self, string: &str) -> Result<()> {
let mut pos = self.position;
if pos + 2 > self.buf.len() {
return Err(UsbError::BufferOverflow);
}
self.buf[pos] = 0;
self.buf[pos + 1] = descriptor_type::STRING;
pos += 2;
for c in string.encode_utf16() {
if pos >= self.buf.len() {
return Err(UsbError::BufferOverflow);
}
self.buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
pos += 2;
}
self.buf[self.position] = (pos - self.position) as u8;
self.position = pos;
Ok(())
}
}