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
232
233
234
235
236
237
238
239
240
use bitfield::bitfield;
use core::fmt::{Debug, Error as FmtError, Formatter};
use core::mem;
use core::ptr::null_mut;
type FmtResult = Result<(), FmtError>;
bitfield! {
struct PckSize(u32);
impl Debug;
pub byte_count, set_byte_count: 13, 0;
pub multi_packet_size, set_multi_packet_size: 27, 14;
pub size, set_size: 30, 28;
pub auto_zlp, set_auto_zlp : 31;
}
bitfield! {
struct ExtReg(u16);
impl Debug;
pub subpid, set_subpid: 3, 0;
pub link_state, set_link_state: 7, 4;
pub besl, set_besl: 11, 8;
pub remote_wake, set_remote_wake: 12;
}
bitfield! {
struct StatusBk(u8);
impl Debug;
pub crc_error, set_crc_error: 0;
pub error_flow, set_error_flow: 1;
}
#[repr(C)]
#[derive(Debug)]
pub struct DeviceDescBank {
addr: *mut u8,
pcksize: PckSize,
extreg: ExtReg,
status_bk: StatusBk,
_reserved: [u8; 5],
}
impl DeviceDescBank {
fn new() -> Self {
debug_assert_eq!(16, mem::size_of::<DeviceDescBank>());
Self {
addr: null_mut(),
pcksize: PckSize(0),
extreg: ExtReg(0),
status_bk: StatusBk(0),
_reserved: [0, 0, 0, 0, 0],
}
}
#[allow(unused)]
#[allow(unused_variables)]
pub fn set_auto_zlp(&mut self, enable: bool) {
self.pcksize.set_auto_zlp(enable);
}
pub fn set_endpoint_size(&mut self, size: u16) {
let size = match size {
1..=8 => 0u32,
9..=16 => 1,
17..=32 => 2,
33..=64 => 3,
65..=128 => 4,
129..=256 => 5,
257..=512 => 6,
513..=1023 => 7,
_ => unreachable!(),
};
self.pcksize.set_size(size);
}
#[allow(unused)]
pub fn get_endpoint_size(&self) -> u16 {
let bits = self.pcksize.size();
match bits {
0 => 8,
1 => 16,
2 => 32,
3 => 64,
4 => 128,
5 => 256,
6 => 512,
7 => 1023,
_ => unreachable!(),
}
}
pub fn set_multi_packet_size(&mut self, size: u16) {
self.pcksize.set_multi_packet_size(size.into());
}
#[allow(dead_code)]
pub fn get_multi_packet_size(&self) -> u16 {
self.pcksize.multi_packet_size() as u16
}
pub fn set_byte_count(&mut self, size: u16) {
self.pcksize.set_byte_count(size.into());
}
pub fn get_byte_count(&self) -> u16 {
self.pcksize.byte_count() as u16
}
#[allow(unused)]
pub fn link_state(&self) -> u8 {
self.extreg.link_state() as u8
}
#[allow(unused)]
pub fn besl(&self) -> u8 {
self.extreg.besl() as u8
}
#[allow(unused)]
pub fn remote_wake(&self) -> bool {
self.extreg.remote_wake()
}
#[allow(unused)]
pub fn subpid(&self) -> u8 {
self.extreg.subpid() as u8
}
#[allow(unused)]
pub fn error_flow(&self) -> bool {
self.status_bk.error_flow()
}
#[allow(unused)]
pub fn crc_error(&self) -> bool {
self.status_bk.crc_error()
}
pub fn set_address(&mut self, address: *mut u8) {
self.addr = address;
}
pub fn get_address(&self) -> *mut u8 {
self.addr
}
}
#[derive(Debug)]
#[repr(C)]
pub struct DeviceDescriptor {
bank: [DeviceDescBank; 2],
}
impl DeviceDescriptor {
fn new() -> Self {
debug_assert_eq!(32, mem::size_of::<DeviceDescriptor>());
Self {
bank: [DeviceDescBank::new(), DeviceDescBank::new()],
}
}
}
pub struct Descriptors {
desc: [DeviceDescriptor; 8],
}
impl Debug for Descriptors {
fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
for ep in 0..8 {
write!(fmt, "\nep{}: {:?}", ep, &self.desc[ep])?;
}
Ok(())
}
}
impl Descriptors {
pub fn new() -> Self {
Self {
desc: [
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
DeviceDescriptor::new(),
],
}
}
pub fn address(&self) -> u32 {
&self.desc as *const _ as u32
}
pub fn bank(&mut self, idx: usize, bank: usize) -> &mut DeviceDescBank {
&mut self.desc[idx].bank[bank]
}
}
unsafe impl Send for DeviceDescBank {}