-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
689 lines (602 loc) · 24.4 KB
/
mod.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
pub mod command_packet;
pub mod commands;
mod descriptors;
pub mod endpoint;
mod packet_queue;
mod pma;
mod setup_packet;
pub mod usb_error;
use self::{
command_packet::CommandPacket,
descriptors::*,
endpoint::{
ControlEndpointStatus, DeviceEndpoint, EndpointDirection, EndpointStatus, EndpointType,
},
packet_queue::PacketQueue,
pma::PacketMemoryArea,
setup_packet::{Request, RequestKind, RequestRecipient, SetupPacket},
};
use array::Array;
use core::convert::TryFrom;
pub const SUPPORTED_ENDPOINTS: [EndpointType; 3] = [
EndpointType::Control,
EndpointType::Device(DeviceEndpoint::System),
EndpointType::Device(DeviceEndpoint::Keyboard),
];
#[derive(Copy, Clone)]
enum DeviceStatus {
// Device hasn't been started yet, starting, or has been disconnected.
Default,
// We've received an address from the host.
Addressed,
// Enumeration is complete, we can talk to the host.
Configured,
}
/// Possible interrupt types.
pub enum UsbInterrupt {
Reset,
Error,
CorrectTransfer,
SuspendSoFEsoF,
}
pub struct TransactionFlags {
pub setup: bool,
pub rx: bool,
pub tx: bool,
}
pub struct Transaction {
pub endpoint: EndpointType,
pub direction: EndpointDirection,
pub flags: TransactionFlags,
}
#[derive(Copy, Clone)]
pub struct UsbState {
device_status: DeviceStatus,
control_endpoint_status: ControlEndpointStatus,
packets: PacketQueue,
address: u8,
configuration_index: u8,
protocol: u8,
idle_state: u8,
alt_setting: u8,
pub command: Option<CommandPacket>,
}
impl Default for UsbState {
fn default() -> Self {
UsbState {
device_status: DeviceStatus::Default,
control_endpoint_status: ControlEndpointStatus::Idle,
packets: PacketQueue::new(),
address: 0,
configuration_index: 0,
protocol: 0,
idle_state: 0,
alt_setting: 0,
command: None,
}
}
}
// Describes USB hardware management interface.
pub trait USBHardware {
/// Initializes hardware if needed.
fn setup(&self);
/// Releases hardware if needed.
fn teardown(&self);
/// Enables USB device.
fn enable(&self);
/// Returns start address of the BTABLE.
fn btable_address(&self) -> usize;
/// Used to retrieve transaction that has been completed and caused `Correct Transfer` interrupt.
fn transaction(&self) -> Transaction;
/// Sets status for the specified endpoint.
fn set_endpoint_status(
&self,
endpoint: EndpointType,
direction: EndpointDirection,
status: EndpointStatus,
);
/// Assigns specified address to the USB device.
fn set_address(&self, address: u8);
/// Opens specified endpoint.
fn open_endpoint(&self, endpoint: EndpointType);
/// Closes specified endpoint.
fn close_endpoint(&self, endpoint: EndpointType);
/// Checks whether specified interrupt is active.
fn is_interrupt_active(&self, interrupt: UsbInterrupt) -> bool;
/// Tells hardware that specified USB interrupt has been handled.
fn mark_interrupt_as_handled(&self, interrupt: UsbInterrupt);
/// Tells USB peripheral that specific transaction has been successfully handled.
fn mark_transaction_as_handled(&self, endpoint: EndpointType, direction: EndpointDirection);
}
pub struct USB<'a, T: USBHardware> {
hw: &'a T,
pma: PacketMemoryArea,
state: &'a mut UsbState,
}
impl<'a, T: USBHardware> USB<'a, T> {
pub fn new(hw: &'a T, state: &'a mut UsbState) -> Self {
let base_address = hw.btable_address();
USB {
hw,
pma: PacketMemoryArea { base_address },
state,
}
}
/// Prepares and starts USB hardware (setup clocks, SOF etc.).
pub fn setup(&mut self) {
self.hw.setup();
self.pma.init(&SUPPORTED_ENDPOINTS);
self.state.address = 0;
self.update_device_status(DeviceStatus::Default);
}
/// Stops and releases USB hardware (stops clocks, disables interrupts etc.).
pub fn teardown(&mut self) {
SUPPORTED_ENDPOINTS
.iter()
.for_each(|endpoint| self.hw.close_endpoint(*endpoint));
self.state.address = 0;
self.update_device_status(DeviceStatus::Default);
self.hw.teardown();
}
pub fn interrupt(&mut self) {
if self.hw.is_interrupt_active(UsbInterrupt::Reset) {
self.reset();
}
if self.hw.is_interrupt_active(UsbInterrupt::Error) {
self.hw.mark_interrupt_as_handled(UsbInterrupt::Error);
}
// Clear SUSP, SOF and ESOF
self.hw
.mark_interrupt_as_handled(UsbInterrupt::SuspendSoFEsoF);
// Correct endpoint transfer
if self.hw.is_interrupt_active(UsbInterrupt::CorrectTransfer) {
self.correct_transfer();
}
}
/// Sends report via specified Device endpoint.
pub fn send(&mut self, endpoint: DeviceEndpoint, data: &[u8]) {
self.send_data(EndpointType::Device(endpoint), &data);
}
fn correct_transfer(&mut self) {
// USB_ISTR_CTR is read only and will be automatically cleared by hardware when we process
// all endpoint results.
while self.hw.is_interrupt_active(UsbInterrupt::CorrectTransfer) {
let transaction = self.hw.transaction();
match (&transaction.endpoint, &transaction.direction) {
(EndpointType::Control, EndpointDirection::Receive) => {
self.handle_control_out_transfer(&transaction)
}
(EndpointType::Control, EndpointDirection::Transmit) => {
self.handle_control_in_transfer(&transaction)
}
(EndpointType::Device(_), EndpointDirection::Receive) => {
self.handle_device_out_transfer(&transaction)
}
(EndpointType::Device(_), EndpointDirection::Transmit) => {
self.handle_device_in_transfer(&transaction)
}
}
}
}
fn handle_control_out_transfer(&mut self, transaction: &Transaction) {
if transaction.flags.setup {
self.handle_control_setup_out_transfer(transaction);
} else if transaction.flags.rx {
self.handle_control_data_out_transfer(transaction);
}
}
fn handle_control_setup_out_transfer(&mut self, transaction: &Transaction) {
let setup_packet = SetupPacket::from((
self.pma.read(transaction.endpoint, 0),
self.pma.read(transaction.endpoint, 2),
self.pma.read(transaction.endpoint, 4),
self.pma.read(transaction.endpoint, 6),
));
self.hw
.mark_transaction_as_handled(transaction.endpoint, transaction.direction);
self.update_control_endpoint_status(ControlEndpointStatus::Setup);
match setup_packet.recipient {
RequestRecipient::Device => self.handle_device_request(setup_packet),
RequestRecipient::Interface => self.handle_interface_request(setup_packet),
RequestRecipient::Endpoint => self.handle_endpoint_request(setup_packet),
_ => self.hw.set_endpoint_status(
transaction.endpoint,
transaction.direction,
EndpointStatus::Stall,
),
}
}
fn handle_control_data_out_transfer(&self, transaction: &Transaction) {
self.hw
.mark_transaction_as_handled(transaction.endpoint, transaction.direction);
// Here we can check the amount of data and do smth with it....
self.pma.set_rx_count(transaction.endpoint, 0);
self.hw.set_endpoint_status(
transaction.endpoint,
transaction.direction,
EndpointStatus::Valid,
);
}
fn handle_control_in_transfer(&mut self, transaction: &Transaction) {
self.hw
.mark_transaction_as_handled(transaction.endpoint, transaction.direction);
// If we have pending packets, continue to send to them.
if let Some(packet) = self.state.packets.dequeue(transaction.endpoint) {
self.send_packet(transaction.endpoint, packet.as_ref());
return;
}
if let ControlEndpointStatus::DataIn = self.state.control_endpoint_status {
self.update_control_endpoint_status(ControlEndpointStatus::DataOut);
// Prepare for premature end of transfer.
self.pma.set_rx_count(transaction.endpoint, 0);
self.hw.set_endpoint_status(
transaction.endpoint,
EndpointDirection::Receive,
EndpointStatus::Valid,
);
}
if self.state.address > 0 {
self.hw.set_address(self.state.address);
self.state.address = 0;
}
}
fn handle_device_out_transfer(&mut self, transaction: &Transaction) {
self.hw
.mark_transaction_as_handled(transaction.endpoint, transaction.direction);
let command_packet_length = self.pma.rx_count(transaction.endpoint) as usize;
let mut command_byte_array = Array::new();
for index in (0..command_packet_length).step_by(2) {
let half_word = self.pma.read(transaction.endpoint, index as u16);
command_byte_array.push((half_word & 0x00ff) as u8);
// It's possible to receive odd number of bytes, and second part of `u16` will contain
// some garbage value we don't want to pick up.
if command_byte_array.len() < command_packet_length {
command_byte_array.push(((half_word & 0xff00) >> 8) as u8);
}
}
self.state.command = CommandPacket::try_from(command_byte_array).ok();
self.pma.set_rx_count(transaction.endpoint, 0);
self.hw.set_endpoint_status(
transaction.endpoint,
EndpointDirection::Receive,
EndpointStatus::Valid,
);
}
fn handle_device_in_transfer(&mut self, transaction: &Transaction) {
self.hw
.mark_transaction_as_handled(transaction.endpoint, transaction.direction);
// If we have pending packets, continue to send to them.
if let Some(packet) = self.state.packets.dequeue(transaction.endpoint) {
self.send_packet(transaction.endpoint, packet.as_ref());
}
}
fn update_address(&mut self, address: u8) {
if address == 0 {
self.hw.enable();
}
self.state.address = address;
}
fn send_data(&mut self, endpoint_type: EndpointType, data: &[u8]) {
// If data to send is larger than maximum packet size, let's send the first chunk and put
// the rest of the data into packet queue.
let packet = if data.len() > MAX_PACKET_SIZE {
self.state
.packets
.enqueue(endpoint_type, &data[MAX_PACKET_SIZE..]);
&data[..MAX_PACKET_SIZE]
} else {
self.state.packets.clear(endpoint_type);
data
};
self.send_packet(endpoint_type, packet);
}
fn send_packet<'p, P: IntoIterator<Item = &'p u8>>(
&mut self,
endpoint_type: EndpointType,
packet: P,
) {
self.pma
.set_tx_count(endpoint_type, self.pma.write(endpoint_type, packet) as u16);
// Now that the PMA memory is prepared,tell the peripheral to send it.
self.hw.set_endpoint_status(
endpoint_type,
EndpointDirection::Transmit,
EndpointStatus::Valid,
);
}
fn send_control_data(&mut self, data: &[u8]) {
self.update_control_endpoint_status(ControlEndpointStatus::DataIn);
self.send_data(EndpointType::Control, data);
}
fn send_control_zero_length_packet(&mut self) {
self.update_control_endpoint_status(ControlEndpointStatus::StatusIn);
self.send_data(EndpointType::Control, &[]);
}
fn reset(&mut self) {
self.hw.mark_interrupt_as_handled(UsbInterrupt::Reset);
self.update_address(0);
self.hw.open_endpoint(EndpointType::Control);
}
fn update_device_status(&mut self, device_status: DeviceStatus) {
self.state.device_status = device_status
}
fn update_control_endpoint_status(&mut self, control_endpoint_status: ControlEndpointStatus) {
self.state.control_endpoint_status = control_endpoint_status;
}
fn stall_endpoint(&self, endpoint_address: u8) {
match EndpointType::try_from(endpoint_address & 0x7f) {
Ok(EndpointType::Control) => {
self.control_endpoint_error();
}
Ok(endpoint) => {
let direction = if endpoint_address & 0x80 == 0x80 {
EndpointDirection::Transmit
} else {
EndpointDirection::Receive
};
self.hw
.set_endpoint_status(endpoint, direction, EndpointStatus::Stall);
}
_ => {}
};
}
fn unstall_endpoint(&self, endpoint_address: u8) {
if let Ok(endpoint) = EndpointType::try_from(endpoint_address & 0x7f) {
let direction = if endpoint.is_control() || endpoint_address & 0x80 == 0x80 {
EndpointDirection::Transmit
} else {
EndpointDirection::Receive
};
self.hw
.set_endpoint_status(endpoint, direction, EndpointStatus::Stall);
}
}
fn handle_endpoint_request(&mut self, request_header: SetupPacket) {
let endpoint_address = request_header.index as u8;
let is_device_endpoint = endpoint_address & 0x7f != 0;
match (request_header.request, self.state.device_status) {
(Request::SetFeature, DeviceStatus::Addressed)
| (Request::ClearFeature, DeviceStatus::Addressed) => {
if is_device_endpoint {
self.stall_endpoint(endpoint_address);
}
}
(Request::SetFeature, DeviceStatus::Configured) => {
// USB_FEATURE_EP_HALT
if request_header.value == 0 && is_device_endpoint {
self.stall_endpoint(endpoint_address);
}
self.send_control_zero_length_packet();
}
(Request::ClearFeature, DeviceStatus::Configured) => {
// USB_FEATURE_EP_HALT
if request_header.value == 0 && is_device_endpoint {
self.unstall_endpoint(endpoint_address);
}
}
(Request::GetStatus, DeviceStatus::Configured)
| (Request::GetStatus, DeviceStatus::Addressed) => {
// SHOULD BE: status=isStalled(ep_addr) ? 1 : 0; sendControlData(&status,2);
self.send_control_data(&[0x0, 0x0]);
}
(Request::SetFeature, _) | (Request::ClearFeature, _) | (Request::GetStatus, _) => {
self.control_endpoint_error()
}
_ => {}
}
}
fn handle_device_request(&mut self, request_header: SetupPacket) {
match request_header.request {
Request::GetDescriptor => self.handle_get_descriptor(request_header),
Request::SetAddress => self.handle_set_address(request_header),
Request::SetConfiguration => self.handle_set_configuration(request_header),
Request::GetConfiguration => self.handle_get_configuration(request_header),
Request::GetStatus => self.handle_get_status(),
Request::SetFeature => self.handle_set_feature(request_header),
Request::ClearFeature => self.handle_clear_feature(request_header),
_ => self.control_endpoint_error(),
}
}
fn handle_get_descriptor(&mut self, request_header: SetupPacket) {
// See USB 2.0 Specification, Table 9-5. Descriptor Types
let data_to_send: Option<&[u8]> =
match (request_header.value >> 8, request_header.value & 0xff) {
(0x1, _) => Some(&DEV_DESC),
(0x2, _) => Some(&CONF_DESC),
(0x3, 0x0) => Some(&LANG_ID_DESCRIPTOR),
// 0x1 - 0x3 Based on values in Device descriptor.
(0x3, 0x1) => Some(&MANUFACTURER_STR),
(0x3, 0x2) => Some(&PRODUCT_STR),
(0x3, 0x3) => Some(&SERIAL_NUMBER_STR),
// 0x4 - Based on value in Config descriptor (iConfiguration)
(0x3, 0x4) => Some(&CONF_STR),
// 0x5 - Based on value in Interface descriptor (iInterface)
(0x3, 0x5) => Some(&INTERFACE_STR),
_ => None,
};
if let Some(data) = data_to_send {
let data_length = data.len();
if request_header.length > 0 && data_length > 0 {
// Send the data to the host.
let data_to_send_length = if data_length <= request_header.length as usize {
data_length
} else {
request_header.length as usize
};
self.send_control_data(&data[..data_to_send_length]);
}
} else {
self.control_endpoint_error();
}
}
fn handle_set_address(&mut self, request_header: SetupPacket) {
if request_header.index == 0 && request_header.length == 0 {
if let DeviceStatus::Configured = self.state.device_status {
self.control_endpoint_error();
} else {
let address = (request_header.value & 0x7F) as u8;
self.update_address(address);
self.send_control_zero_length_packet();
self.update_device_status(if address != 0 {
DeviceStatus::Addressed
} else {
DeviceStatus::Default
});
}
} else {
self.control_endpoint_error();
}
}
fn handle_set_configuration(&mut self, request_header: SetupPacket) {
let configuration_index = request_header.value as u8;
self.state.configuration_index = configuration_index;
if configuration_index > 1 || matches!(self.state.device_status, DeviceStatus::Default) {
self.control_endpoint_error();
return;
}
let device_status = match self.state.device_status {
DeviceStatus::Addressed if configuration_index != 0 => {
self.hw
.open_endpoint(EndpointType::Device(DeviceEndpoint::System));
self.hw
.open_endpoint(EndpointType::Device(DeviceEndpoint::Keyboard));
DeviceStatus::Configured
}
DeviceStatus::Configured if configuration_index == 0 => {
self.hw
.close_endpoint(EndpointType::Device(DeviceEndpoint::System));
self.hw
.close_endpoint(EndpointType::Device(DeviceEndpoint::Keyboard));
DeviceStatus::Addressed
}
_ => self.state.device_status,
};
self.send_control_zero_length_packet();
self.update_device_status(device_status);
}
fn handle_get_configuration(&mut self, request_header: SetupPacket) {
if request_header.length != 1 {
self.control_endpoint_error();
} else {
match self.state.device_status {
DeviceStatus::Addressed => {
self.state.configuration_index = 0;
self.send_control_data(&[0]);
}
DeviceStatus::Configured => {
self.send_control_data(&[self.state.configuration_index])
}
_ => self.control_endpoint_error(),
}
}
}
fn handle_get_status(&mut self) {
if let DeviceStatus::Addressed | DeviceStatus::Configured = self.state.device_status {
// Bus powered, supports remote wakeup.
self.send_control_data(&[0x2, 0x0]);
}
}
fn handle_set_feature(&mut self, request_header: SetupPacket) {
if request_header.value == 1 {
// ACK
self.send_control_zero_length_packet();
}
}
fn handle_clear_feature(&mut self, request_header: SetupPacket) {
if let DeviceStatus::Addressed | DeviceStatus::Configured = self.state.device_status {
if request_header.value == 1 {
// ACK
self.send_control_zero_length_packet();
}
} else {
self.control_endpoint_error();
}
}
fn handle_interface_request(&mut self, request_header: SetupPacket) {
match (self.state.device_status, &request_header.kind) {
(DeviceStatus::Configured, RequestKind::Standard) => {
self.handle_standard_setup(request_header)
}
(DeviceStatus::Configured, RequestKind::Class) => {
self.handle_class_setup(request_header)
}
_ => self.control_endpoint_error(),
}
}
fn handle_standard_setup(&mut self, request_header: SetupPacket) {
match request_header.request {
// See HID Spec 7.1: the HID class uses the standard request `Get_Descriptor` as
// described in the USB Specification.
Request::GetDescriptor => {
let ack_data = [];
// Value is 2 bytes value, we need only high byte:
let data = match (request_header.value >> 8, request_header.index) {
// USB_DESC_TYPE_HID_DESCRIPTOR (HID)
(0x21, 0) => get_hid_descriptor(DeviceEndpoint::System),
(0x21, 1) => get_hid_descriptor(DeviceEndpoint::Keyboard),
// USB_DESC_TYPE_HID_REPORT (Report)
(0x22, 0) => get_hid_report_descriptor(DeviceEndpoint::System),
(0x22, 1) => get_hid_report_descriptor(DeviceEndpoint::Keyboard),
// 0x23 - Physical descriptor, 0x24 - 0x2F Reserved or unknown interface.
_ => ack_data.as_ref(),
};
let report_length = request_header.length as usize;
self.send_control_data(if report_length < data.len() {
&data[..report_length]
} else {
&data
});
}
Request::GetStatus => self.send_control_data(&[0x0, 0x0]),
Request::SetInterface => {
self.state.alt_setting = request_header.value as u8;
self.send_control_zero_length_packet();
}
Request::GetInterface => self.send_control_data(&[self.state.alt_setting]),
_ => self.control_endpoint_error(),
}
}
fn handle_class_setup(&mut self, request_header: SetupPacket) {
match request_header.request {
// CUSTOM_HID_REQ_GET_IDLE
Request::Two => self.send_control_data(&[self.state.idle_state]),
// CUSTOM_HID_REQ_GET_PROTOCOL
Request::SetFeature => self.send_control_data(&[self.state.protocol]),
// CUSTOM_HID_REQ_SET_REPORT
Request::SetConfiguration => {
self.update_control_endpoint_status(ControlEndpointStatus::DataOut);
self.pma
.set_rx_count(EndpointType::Control, request_header.length);
self.hw.set_endpoint_status(
EndpointType::Control,
EndpointDirection::Receive,
EndpointStatus::Valid,
);
self.send_control_zero_length_packet();
}
// CUSTOM_HID_REQ_SET_IDLE
Request::GetInterface => {
self.state.idle_state = (request_header.value >> 8) as u8;
self.send_control_zero_length_packet();
}
// CUSTOM_HID_REQ_SET_PROTOCOL
Request::SetInterface => {
self.state.protocol = request_header.value as u8;
self.send_control_zero_length_packet();
}
_ => self.control_endpoint_error(),
}
}
fn control_endpoint_error(&self) {
self.hw.set_endpoint_status(
EndpointType::Control,
EndpointDirection::Receive,
EndpointStatus::Stall,
);
self.hw.set_endpoint_status(
EndpointType::Control,
EndpointDirection::Transmit,
EndpointStatus::Stall,
);
}
}