-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.rs
1633 lines (1471 loc) · 63.5 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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
// TODO: rewrite this entire driver because all the enums I'm defining are causing problems
use alloc::sync::Arc;
use bit_field::BitField;
use conquer_once::spin::OnceCell;
use core::{
ptr::addr_of,
sync::atomic::{AtomicUsize, Ordering},
};
use num_traits::cast::FromPrimitive;
use x86_64::{
structures::paging::{Page, Size4KiB},
VirtAddr,
};
use crate::{
common::addralloc,
common::XhciMapper,
pci_impl::{register_device_driver, DeviceKind, FOSSPciDeviceHandle, PCI_TABLE},
xhci::mass_storage::UsbDeviceKind,
};
use log::info;
use pcics::{header::HeaderType, Header};
use spin::{Once, RwLock};
use xhci::{
accessor::array::ReadWrite,
context::{Device, EndpointType, Input, InputHandler, SlotState},
extended_capabilities::List,
registers::{
doorbell::Register, Capability, InterrupterRegisterSet, Operational, PortRegisterSet,
Runtime,
},
ring::trb::{
command::{
AddressDevice, ConfigureEndpoint, DisableSlot, EnableSlot, EvaluateContext, ForceEvent,
ForceHeader, GetExtendedProperty, GetPortBandwidth, NegotiateBandwidth,
Noop as CmdNoop, ResetDevice, ResetEndpoint, SetExtendedProperty,
SetLatencyToleranceValue, SetTrDequeuePointer, StopEndpoint,
},
event::{
BandwidthRequest, CommandCompletion, CompletionCode, DeviceNotification, Doorbell,
HostController, MfindexWrap, PortStatusChange, TransferEvent,
},
transfer::{
DataStage, EventData, Isoch, Noop as TransferNoop, Normal, SetupStage, StatusStage,
},
Link, Type as TrbType,
},
Registers,
};
pub mod mass_storage;
pub static ROOT_LINK: OnceCell<RwLock<Link>> = OnceCell::uninit();
pub(crate) static MAPPER: RwLock<XhciMapper> = RwLock::new(XhciMapper);
/// Helper trait for parsing the specific TRBs we're dealing with
pub trait TrbAnalyzer: AsRef<[u32]> {
fn get_type(&self) -> TrbType {
match self.as_ref()[3].get_bits(10..=15) {
// TODO: figure out why QEMU is giving 0 when I attempt a device probe
0 => TrbType::Normal,
1 => TrbType::Normal,
2 => TrbType::SetupStage,
3 => TrbType::DataStage,
4 => TrbType::StatusStage,
5 => TrbType::Isoch,
6 => TrbType::Link,
7 => TrbType::EventData,
8 => TrbType::NoopTransfer,
9 => TrbType::EnableSlot,
10 => TrbType::DisableSlot,
11 => TrbType::AddressDevice,
12 => TrbType::ConfigureEndpoint,
13 => TrbType::EvaluateContext,
14 => TrbType::ResetEndpoint,
15 => TrbType::StopEndpoint,
16 => TrbType::SetTrDequeuePointer,
17 => TrbType::ResetDevice,
18 => TrbType::ForceEvent,
19 => TrbType::NegotiateBandwidth,
20 => TrbType::SetLatencyToleranceValue,
21 => TrbType::GetPortBandwidth,
22 => TrbType::ForceHeader,
23 => TrbType::NoopCommand,
24 => TrbType::GetExtendedProperty,
25 => TrbType::SetExtendedProperty,
32 => TrbType::TransferEvent,
33 => TrbType::CommandCompletion,
34 => TrbType::PortStatusChange,
35 => TrbType::BandwidthRequest,
36 => TrbType::Doorbell,
37 => TrbType::HostController,
38 => TrbType::DeviceNotification,
39 => TrbType::MfindexWrap,
_ => panic!(
"Invalid TRB type: {:#?}",
self.as_ref()[3].get_bits(10..=15)
),
}
}
}
impl TrbAnalyzer for AddressDevice {}
impl TrbAnalyzer for ConfigureEndpoint {}
impl TrbAnalyzer for DisableSlot {}
impl TrbAnalyzer for EnableSlot {}
impl TrbAnalyzer for EvaluateContext {}
impl TrbAnalyzer for ForceEvent {}
impl TrbAnalyzer for ForceHeader {}
impl TrbAnalyzer for GetExtendedProperty {}
impl TrbAnalyzer for GetPortBandwidth {}
impl TrbAnalyzer for NegotiateBandwidth {}
impl TrbAnalyzer for CmdNoop {}
impl TrbAnalyzer for ResetDevice {}
impl TrbAnalyzer for ResetEndpoint {}
impl TrbAnalyzer for SetExtendedProperty {}
impl TrbAnalyzer for SetLatencyToleranceValue {}
impl TrbAnalyzer for SetTrDequeuePointer {}
impl TrbAnalyzer for StopEndpoint {}
impl TrbAnalyzer for BandwidthRequest {}
impl TrbAnalyzer for CommandCompletion {}
impl TrbAnalyzer for DeviceNotification {}
impl TrbAnalyzer for Doorbell {}
impl TrbAnalyzer for HostController {}
impl TrbAnalyzer for MfindexWrap {}
impl TrbAnalyzer for PortStatusChange {}
impl TrbAnalyzer for TransferEvent {}
impl TrbAnalyzer for DataStage {}
impl TrbAnalyzer for EventData {}
impl TrbAnalyzer for Isoch {}
impl TrbAnalyzer for TransferNoop {}
impl TrbAnalyzer for Normal {}
impl TrbAnalyzer for SetupStage {}
impl TrbAnalyzer for StatusStage {}
impl TrbAnalyzer for Link {}
impl TrbAnalyzer for &'static mut AddressDevice {}
impl TrbAnalyzer for &'static mut ConfigureEndpoint {}
impl TrbAnalyzer for &'static mut DisableSlot {}
impl TrbAnalyzer for &'static mut EnableSlot {}
impl TrbAnalyzer for &'static mut EvaluateContext {}
impl TrbAnalyzer for &'static mut ForceEvent {}
impl TrbAnalyzer for &'static mut ForceHeader {}
impl TrbAnalyzer for &'static mut GetExtendedProperty {}
impl TrbAnalyzer for &'static mut GetPortBandwidth {}
impl TrbAnalyzer for &'static mut NegotiateBandwidth {}
impl TrbAnalyzer for &'static mut CmdNoop {}
impl TrbAnalyzer for &'static mut ResetDevice {}
impl TrbAnalyzer for &'static mut ResetEndpoint {}
impl TrbAnalyzer for &'static mut SetExtendedProperty {}
impl TrbAnalyzer for &'static mut SetLatencyToleranceValue {}
impl TrbAnalyzer for &'static mut SetTrDequeuePointer {}
impl TrbAnalyzer for &'static mut StopEndpoint {}
impl TrbAnalyzer for &'static mut BandwidthRequest {}
impl TrbAnalyzer for &'static mut CommandCompletion {}
impl TrbAnalyzer for &'static mut DeviceNotification {}
impl TrbAnalyzer for &'static mut Doorbell {}
impl TrbAnalyzer for &'static mut HostController {}
impl TrbAnalyzer for &'static mut MfindexWrap {}
impl TrbAnalyzer for &'static mut PortStatusChange {}
impl TrbAnalyzer for &'static mut TransferEvent {}
impl TrbAnalyzer for &'static mut DataStage {}
impl TrbAnalyzer for &'static mut EventData {}
impl TrbAnalyzer for &'static mut Isoch {}
impl TrbAnalyzer for &'static mut TransferNoop {}
impl TrbAnalyzer for &'static mut Normal {}
impl TrbAnalyzer for &'static mut SetupStage {}
impl TrbAnalyzer for &'static mut StatusStage {}
#[derive(Debug)]
pub enum CommandKind<'a> {
AddressDevice(&'a mut AddressDevice),
ConfigureEndpoint(&'a mut ConfigureEndpoint),
DisableSlot(&'a mut DisableSlot),
EnableSlot(&'a mut EnableSlot),
EvaluateContext(&'a mut EvaluateContext),
ForceEvent(&'a mut ForceEvent),
ForceHeader(&'a mut ForceHeader),
GetExtendedProperty(&'a mut GetExtendedProperty),
GetPortBandwidth(&'a mut GetPortBandwidth),
NegotiateBandwidth(&'a mut NegotiateBandwidth),
CmdNoop(&'a mut CmdNoop),
ResetDevice(&'a mut ResetDevice),
ResetEndpoint(&'a mut ResetEndpoint),
SetExtendedProperty(&'a mut SetExtendedProperty),
SetLatencyToleranceValue(&'a mut SetLatencyToleranceValue),
SetTrDequeuePointer(&'a mut SetTrDequeuePointer),
StopEndpoint(&'a mut StopEndpoint),
}
#[derive(Debug)]
pub enum EventKind<'a> {
BandwidthRequest(&'a mut BandwidthRequest),
CommandCompletion(&'a mut CommandCompletion),
DeviceNotification(&'a mut DeviceNotification),
Doorbell(&'a mut Doorbell),
HostController(&'a mut HostController),
MfindexWrap(&'a mut MfindexWrap),
PortStatusChange(&'a mut PortStatusChange),
TransferEvent(&'a mut TransferEvent),
}
pub enum TransferKind<'a> {
DataStage(&'a mut DataStage),
EventData(&'a mut EventData),
Isoch(&'a mut Isoch),
TransferNoop(&'a mut TransferNoop),
Normal(&'a mut Normal),
SetupStage(&'a mut SetupStage),
StatusStage(&'a mut StatusStage),
}
pub enum TrbKind<'a> {
Command(CommandKind<'a>),
Event(EventKind<'a>),
Transfer(TransferKind<'a>),
Link(&'a mut Link),
}
macro_rules! impl_from_ref_for_kind {
($sub:ident, $kind:ident) => {
impl<'a> From<&'a mut $sub> for $kind<'a> {
fn from(sub: &'a mut $sub) -> Self {
$kind::$sub(sub)
}
}
};
}
impl_from_ref_for_kind!(AddressDevice, CommandKind);
impl_from_ref_for_kind!(ConfigureEndpoint, CommandKind);
impl_from_ref_for_kind!(DisableSlot, CommandKind);
impl_from_ref_for_kind!(EnableSlot, CommandKind);
impl_from_ref_for_kind!(EvaluateContext, CommandKind);
impl_from_ref_for_kind!(ForceEvent, CommandKind);
impl_from_ref_for_kind!(ForceHeader, CommandKind);
impl_from_ref_for_kind!(GetExtendedProperty, CommandKind);
impl_from_ref_for_kind!(GetPortBandwidth, CommandKind);
impl_from_ref_for_kind!(NegotiateBandwidth, CommandKind);
impl_from_ref_for_kind!(CmdNoop, CommandKind);
impl_from_ref_for_kind!(ResetDevice, CommandKind);
impl_from_ref_for_kind!(ResetEndpoint, CommandKind);
impl_from_ref_for_kind!(SetExtendedProperty, CommandKind);
impl_from_ref_for_kind!(SetLatencyToleranceValue, CommandKind);
impl_from_ref_for_kind!(SetTrDequeuePointer, CommandKind);
impl_from_ref_for_kind!(StopEndpoint, CommandKind);
impl_from_ref_for_kind!(BandwidthRequest, EventKind);
impl_from_ref_for_kind!(CommandCompletion, EventKind);
impl_from_ref_for_kind!(DeviceNotification, EventKind);
impl_from_ref_for_kind!(Doorbell, EventKind);
impl_from_ref_for_kind!(HostController, EventKind);
impl_from_ref_for_kind!(MfindexWrap, EventKind);
impl_from_ref_for_kind!(PortStatusChange, EventKind);
impl_from_ref_for_kind!(TransferEvent, EventKind);
impl_from_ref_for_kind!(DataStage, TransferKind);
impl_from_ref_for_kind!(EventData, TransferKind);
impl_from_ref_for_kind!(Isoch, TransferKind);
impl_from_ref_for_kind!(TransferNoop, TransferKind);
impl_from_ref_for_kind!(Normal, TransferKind);
impl_from_ref_for_kind!(SetupStage, TransferKind);
impl_from_ref_for_kind!(StatusStage, TransferKind);
macro_rules! impl_from_kind_for_trb_kind {
($sub:ident, $kind:ident) => {
impl<'a> From<$sub<'a>> for TrbKind<'a> {
fn from(sub: $sub<'a>) -> Self {
TrbKind::$kind(sub)
}
}
};
}
impl_from_kind_for_trb_kind!(CommandKind, Command);
impl_from_kind_for_trb_kind!(EventKind, Event);
impl_from_kind_for_trb_kind!(TransferKind, Transfer);
impl<'a> From<&'a mut Link> for TrbKind<'a> {
fn from(link: &'a mut Link) -> Self {
TrbKind::Link(link)
}
}
macro_rules! impl_from_ref_for_trb_kind {
($sub:ident, $kind:ident) => {
impl<'a> From<&'a mut $sub> for TrbKind<'a> {
fn from(sub: &'a mut $sub) -> Self {
TrbKind::$kind(sub.into())
}
}
};
}
impl_from_ref_for_trb_kind!(AddressDevice, Command);
impl_from_ref_for_trb_kind!(ConfigureEndpoint, Command);
impl_from_ref_for_trb_kind!(DisableSlot, Command);
impl_from_ref_for_trb_kind!(EnableSlot, Command);
impl_from_ref_for_trb_kind!(EvaluateContext, Command);
impl_from_ref_for_trb_kind!(ForceEvent, Command);
impl_from_ref_for_trb_kind!(ForceHeader, Command);
impl_from_ref_for_trb_kind!(GetExtendedProperty, Command);
impl_from_ref_for_trb_kind!(GetPortBandwidth, Command);
impl_from_ref_for_trb_kind!(NegotiateBandwidth, Command);
impl_from_ref_for_trb_kind!(CmdNoop, Command);
impl_from_ref_for_trb_kind!(ResetDevice, Command);
impl_from_ref_for_trb_kind!(ResetEndpoint, Command);
impl_from_ref_for_trb_kind!(SetExtendedProperty, Command);
impl_from_ref_for_trb_kind!(SetLatencyToleranceValue, Command);
impl_from_ref_for_trb_kind!(SetTrDequeuePointer, Command);
impl_from_ref_for_trb_kind!(StopEndpoint, Command);
impl_from_ref_for_trb_kind!(BandwidthRequest, Event);
impl_from_ref_for_trb_kind!(CommandCompletion, Event);
impl_from_ref_for_trb_kind!(DeviceNotification, Event);
impl_from_ref_for_trb_kind!(Doorbell, Event);
impl_from_ref_for_trb_kind!(HostController, Event);
impl_from_ref_for_trb_kind!(MfindexWrap, Event);
impl_from_ref_for_trb_kind!(PortStatusChange, Event);
impl_from_ref_for_trb_kind!(TransferEvent, Event);
impl_from_ref_for_trb_kind!(DataStage, Transfer);
impl_from_ref_for_trb_kind!(EventData, Transfer);
impl_from_ref_for_trb_kind!(Isoch, Transfer);
impl_from_ref_for_trb_kind!(TransferNoop, Transfer);
impl_from_ref_for_trb_kind!(Normal, Transfer);
impl_from_ref_for_trb_kind!(SetupStage, Transfer);
impl_from_ref_for_trb_kind!(StatusStage, Transfer);
macro_rules! impl_from_ptr_for_kind {
($sub:ident, $kind:ident) => {
impl<'a> From<*mut $sub> for $kind<'a> {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn from(sub: *mut $sub) -> Self {
$kind::$sub(unsafe { &mut *sub })
}
}
};
}
impl_from_ptr_for_kind!(AddressDevice, CommandKind);
impl_from_ptr_for_kind!(ConfigureEndpoint, CommandKind);
impl_from_ptr_for_kind!(DisableSlot, CommandKind);
impl_from_ptr_for_kind!(EnableSlot, CommandKind);
impl_from_ptr_for_kind!(EvaluateContext, CommandKind);
impl_from_ptr_for_kind!(ForceEvent, CommandKind);
impl_from_ptr_for_kind!(ForceHeader, CommandKind);
impl_from_ptr_for_kind!(GetExtendedProperty, CommandKind);
impl_from_ptr_for_kind!(GetPortBandwidth, CommandKind);
impl_from_ptr_for_kind!(NegotiateBandwidth, CommandKind);
impl_from_ptr_for_kind!(CmdNoop, CommandKind);
impl_from_ptr_for_kind!(ResetDevice, CommandKind);
impl_from_ptr_for_kind!(ResetEndpoint, CommandKind);
impl_from_ptr_for_kind!(SetExtendedProperty, CommandKind);
impl_from_ptr_for_kind!(SetLatencyToleranceValue, CommandKind);
impl_from_ptr_for_kind!(SetTrDequeuePointer, CommandKind);
impl_from_ptr_for_kind!(StopEndpoint, CommandKind);
impl_from_ptr_for_kind!(BandwidthRequest, EventKind);
impl_from_ptr_for_kind!(CommandCompletion, EventKind);
impl_from_ptr_for_kind!(DeviceNotification, EventKind);
impl_from_ptr_for_kind!(Doorbell, EventKind);
impl_from_ptr_for_kind!(HostController, EventKind);
impl_from_ptr_for_kind!(MfindexWrap, EventKind);
impl_from_ptr_for_kind!(PortStatusChange, EventKind);
impl_from_ptr_for_kind!(TransferEvent, EventKind);
impl_from_ptr_for_kind!(DataStage, TransferKind);
impl_from_ptr_for_kind!(EventData, TransferKind);
impl_from_ptr_for_kind!(Isoch, TransferKind);
impl_from_ptr_for_kind!(TransferNoop, TransferKind);
impl_from_ptr_for_kind!(Normal, TransferKind);
impl_from_ptr_for_kind!(SetupStage, TransferKind);
impl_from_ptr_for_kind!(StatusStage, TransferKind);
macro_rules! impl_from_ptr_for_trb_kind {
($sub:ident, $kind:ident) => {
impl<'a> From<*mut $sub> for TrbKind<'a> {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn from(sub: *mut $sub) -> Self {
TrbKind::$kind(unsafe { &mut *sub }.into())
}
}
};
}
impl_from_ptr_for_trb_kind!(AddressDevice, Command);
impl_from_ptr_for_trb_kind!(ConfigureEndpoint, Command);
impl_from_ptr_for_trb_kind!(DisableSlot, Command);
impl_from_ptr_for_trb_kind!(EnableSlot, Command);
impl_from_ptr_for_trb_kind!(EvaluateContext, Command);
impl_from_ptr_for_trb_kind!(ForceEvent, Command);
impl_from_ptr_for_trb_kind!(ForceHeader, Command);
impl_from_ptr_for_trb_kind!(GetExtendedProperty, Command);
impl_from_ptr_for_trb_kind!(GetPortBandwidth, Command);
impl_from_ptr_for_trb_kind!(NegotiateBandwidth, Command);
impl_from_ptr_for_trb_kind!(CmdNoop, Command);
impl_from_ptr_for_trb_kind!(ResetDevice, Command);
impl_from_ptr_for_trb_kind!(ResetEndpoint, Command);
impl_from_ptr_for_trb_kind!(SetExtendedProperty, Command);
impl_from_ptr_for_trb_kind!(SetLatencyToleranceValue, Command);
impl_from_ptr_for_trb_kind!(SetTrDequeuePointer, Command);
impl_from_ptr_for_trb_kind!(StopEndpoint, Command);
impl_from_ptr_for_trb_kind!(BandwidthRequest, Event);
impl_from_ptr_for_trb_kind!(CommandCompletion, Event);
impl_from_ptr_for_trb_kind!(DeviceNotification, Event);
impl_from_ptr_for_trb_kind!(Doorbell, Event);
impl_from_ptr_for_trb_kind!(HostController, Event);
impl_from_ptr_for_trb_kind!(MfindexWrap, Event);
impl_from_ptr_for_trb_kind!(PortStatusChange, Event);
impl_from_ptr_for_trb_kind!(TransferEvent, Event);
impl_from_ptr_for_trb_kind!(DataStage, Transfer);
impl_from_ptr_for_trb_kind!(EventData, Transfer);
impl_from_ptr_for_trb_kind!(Isoch, Transfer);
impl_from_ptr_for_trb_kind!(TransferNoop, Transfer);
impl_from_ptr_for_trb_kind!(Normal, Transfer);
impl_from_ptr_for_trb_kind!(SetupStage, Transfer);
impl_from_ptr_for_trb_kind!(StatusStage, Transfer);
impl<'a> CommandKind<'a> {
pub fn as_inner(&'a self) -> &'a dyn TrbAnalyzer {
match self {
CommandKind::AddressDevice(cmd) => *cmd,
CommandKind::ConfigureEndpoint(cmd) => *cmd,
CommandKind::DisableSlot(cmd) => *cmd,
CommandKind::EnableSlot(cmd) => *cmd,
CommandKind::EvaluateContext(cmd) => *cmd,
CommandKind::ForceEvent(cmd) => *cmd,
CommandKind::ForceHeader(cmd) => *cmd,
CommandKind::GetExtendedProperty(cmd) => *cmd,
CommandKind::GetPortBandwidth(cmd) => *cmd,
CommandKind::NegotiateBandwidth(cmd) => *cmd,
CommandKind::CmdNoop(cmd) => *cmd,
CommandKind::ResetDevice(cmd) => *cmd,
CommandKind::ResetEndpoint(cmd) => *cmd,
CommandKind::SetExtendedProperty(cmd) => *cmd,
CommandKind::SetLatencyToleranceValue(cmd) => *cmd,
CommandKind::SetTrDequeuePointer(cmd) => *cmd,
CommandKind::StopEndpoint(cmd) => *cmd,
}
}
pub fn as_ptr(&self) -> *mut dyn TrbAnalyzer {
let self_ptr = self as *const Self as *mut Self;
unsafe { &*self_ptr }.as_inner() as *const _ as *mut _
}
// Using raw pointers on purpose to enable exactly this
#[allow(clippy::mut_from_ref)]
pub fn as_inner_mut(&'a self) -> &'a mut dyn TrbAnalyzer {
unsafe { &mut *(self.as_ptr()) }
}
pub fn cycle_bit(&self) -> bool {
match self {
CommandKind::AddressDevice(cmd) => cmd.cycle_bit(),
CommandKind::ConfigureEndpoint(cmd) => cmd.cycle_bit(),
CommandKind::DisableSlot(cmd) => cmd.cycle_bit(),
CommandKind::EnableSlot(cmd) => cmd.cycle_bit(),
CommandKind::EvaluateContext(cmd) => cmd.cycle_bit(),
CommandKind::ForceEvent(cmd) => cmd.cycle_bit(),
CommandKind::ForceHeader(cmd) => cmd.cycle_bit(),
CommandKind::GetExtendedProperty(cmd) => cmd.cycle_bit(),
CommandKind::GetPortBandwidth(cmd) => cmd.cycle_bit(),
CommandKind::NegotiateBandwidth(cmd) => cmd.cycle_bit(),
CommandKind::CmdNoop(cmd) => cmd.cycle_bit(),
CommandKind::ResetDevice(cmd) => cmd.cycle_bit(),
CommandKind::ResetEndpoint(cmd) => cmd.cycle_bit(),
CommandKind::SetExtendedProperty(cmd) => cmd.cycle_bit(),
CommandKind::SetLatencyToleranceValue(cmd) => cmd.cycle_bit(),
CommandKind::SetTrDequeuePointer(cmd) => cmd.cycle_bit(),
CommandKind::StopEndpoint(cmd) => cmd.cycle_bit(),
}
}
}
impl<'a> EventKind<'a> {
pub fn as_inner(&'a self) -> &'a dyn TrbAnalyzer {
match self {
EventKind::BandwidthRequest(evt) => *evt,
EventKind::CommandCompletion(evt) => *evt,
EventKind::DeviceNotification(evt) => *evt,
EventKind::Doorbell(evt) => *evt,
EventKind::HostController(evt) => *evt,
EventKind::MfindexWrap(evt) => *evt,
EventKind::PortStatusChange(evt) => *evt,
EventKind::TransferEvent(evt) => *evt,
}
}
pub fn as_ptr(&self) -> *mut dyn TrbAnalyzer {
let self_ptr = self as *const Self as *mut Self;
unsafe { &*self_ptr }.as_inner() as *const _ as *mut _
}
// Using raw pointers on purpose to enable exactly this
#[allow(clippy::mut_from_ref)]
pub fn as_inner_mut(&'a self) -> &'a mut dyn TrbAnalyzer {
unsafe { &mut *(self.as_ptr()) }
}
pub fn cycle_bit(&self) -> bool {
match self {
EventKind::BandwidthRequest(evt) => evt.cycle_bit(),
EventKind::CommandCompletion(evt) => evt.cycle_bit(),
EventKind::DeviceNotification(evt) => evt.cycle_bit(),
EventKind::Doorbell(evt) => evt.cycle_bit(),
EventKind::HostController(evt) => evt.cycle_bit(),
EventKind::MfindexWrap(evt) => evt.cycle_bit(),
EventKind::PortStatusChange(evt) => evt.cycle_bit(),
EventKind::TransferEvent(evt) => evt.cycle_bit(),
}
}
}
impl<'a> TransferKind<'a> {
pub fn as_inner(&'a self) -> &'a dyn TrbAnalyzer {
match self {
TransferKind::DataStage(tr) => *tr,
TransferKind::EventData(tr) => *tr,
TransferKind::Isoch(tr) => *tr,
TransferKind::TransferNoop(tr) => *tr,
TransferKind::Normal(tr) => *tr,
TransferKind::SetupStage(tr) => *tr,
TransferKind::StatusStage(tr) => *tr,
}
}
pub fn as_ptr(&self) -> *mut dyn TrbAnalyzer {
let self_ptr = self as *const Self as *mut Self;
unsafe { &*self_ptr }.as_inner() as *const _ as *mut _
}
// Using raw pointers on purpose to enable exactly this
#[allow(clippy::mut_from_ref)]
pub fn as_inner_mut(&'a self) -> &'a mut dyn TrbAnalyzer {
unsafe { &mut *(self.as_ptr()) }
}
pub fn cycle_bit(&self) -> bool {
match self {
TransferKind::DataStage(tr) => tr.cycle_bit(),
TransferKind::EventData(tr) => tr.cycle_bit(),
TransferKind::Isoch(tr) => tr.cycle_bit(),
TransferKind::TransferNoop(tr) => tr.cycle_bit(),
TransferKind::Normal(tr) => tr.cycle_bit(),
TransferKind::SetupStage(tr) => tr.cycle_bit(),
TransferKind::StatusStage(tr) => tr.cycle_bit(),
}
}
}
impl<'a> TrbKind<'a> {
pub fn as_inner(&'a self) -> &'a dyn TrbAnalyzer {
match self {
TrbKind::Command(cmd) => match cmd {
CommandKind::AddressDevice(cmd) => *cmd,
CommandKind::ConfigureEndpoint(cmd) => *cmd,
CommandKind::DisableSlot(cmd) => *cmd,
CommandKind::EnableSlot(cmd) => *cmd,
CommandKind::EvaluateContext(cmd) => *cmd,
CommandKind::ForceEvent(cmd) => *cmd,
CommandKind::ForceHeader(cmd) => *cmd,
CommandKind::GetExtendedProperty(cmd) => *cmd,
CommandKind::GetPortBandwidth(cmd) => *cmd,
CommandKind::NegotiateBandwidth(cmd) => *cmd,
CommandKind::CmdNoop(cmd) => *cmd,
CommandKind::ResetDevice(cmd) => *cmd,
CommandKind::ResetEndpoint(cmd) => *cmd,
CommandKind::SetExtendedProperty(cmd) => *cmd,
CommandKind::SetLatencyToleranceValue(cmd) => *cmd,
CommandKind::SetTrDequeuePointer(cmd) => *cmd,
CommandKind::StopEndpoint(cmd) => *cmd,
},
TrbKind::Event(evt) => match evt {
EventKind::BandwidthRequest(evt) => *evt,
EventKind::CommandCompletion(evt) => *evt,
EventKind::DeviceNotification(evt) => *evt,
EventKind::Doorbell(evt) => *evt,
EventKind::HostController(evt) => *evt,
EventKind::MfindexWrap(evt) => *evt,
EventKind::PortStatusChange(evt) => *evt,
EventKind::TransferEvent(evt) => *evt,
},
TrbKind::Transfer(tr) => match tr {
TransferKind::DataStage(tr) => *tr,
TransferKind::EventData(tr) => *tr,
TransferKind::Isoch(tr) => *tr,
TransferKind::TransferNoop(tr) => *tr,
TransferKind::Normal(tr) => *tr,
TransferKind::SetupStage(tr) => *tr,
TransferKind::StatusStage(tr) => *tr,
},
TrbKind::Link(l) => *l,
}
}
pub fn as_ptr(&'a self) -> *mut dyn TrbAnalyzer {
let self_ptr = self as *const Self as *mut Self;
unsafe { &*self_ptr }.as_inner() as *const _ as *mut _
}
// Using raw pointers on purpose to enable exactly this
#[allow(clippy::mut_from_ref)]
pub fn as_inner_mut(&'a self) -> &'a mut dyn TrbAnalyzer {
unsafe { &mut *(self.as_ptr()) }
}
pub fn cycle_bit(&self) -> bool {
match self {
TrbKind::Command(cmd) => cmd.cycle_bit(),
TrbKind::Event(evt) => evt.cycle_bit(),
TrbKind::Transfer(tr) => tr.cycle_bit(),
TrbKind::Link(l) => l.cycle_bit(),
}
}
}
impl From<*mut dyn TrbAnalyzer> for TrbKind<'_> {
// cannot be implemented any other way
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn from(value: *mut dyn TrbAnalyzer) -> Self {
match unsafe { &*(value) }.get_type() {
TrbType::Normal => TrbKind::from(unsafe { &mut *(value as *mut Normal) }),
TrbType::SetupStage => TrbKind::from(unsafe { &mut *(value as *mut SetupStage) }),
TrbType::DataStage => TrbKind::from(unsafe { &mut *(value as *mut DataStage) }),
TrbType::StatusStage => TrbKind::from(unsafe { &mut *(value as *mut StatusStage) }),
TrbType::Isoch => TrbKind::from(unsafe { &mut *(value as *mut Isoch) }),
TrbType::Link => TrbKind::from(unsafe { &mut *(value as *mut Link) }),
TrbType::EventData => TrbKind::from(unsafe { &mut *(value as *mut EventData) }),
TrbType::NoopTransfer => TrbKind::from(unsafe { &mut *(value as *mut TransferNoop) }),
TrbType::EnableSlot => TrbKind::from(unsafe { &mut *(value as *mut EnableSlot) }),
TrbType::DisableSlot => TrbKind::from(unsafe { &mut *(value as *mut DisableSlot) }),
TrbType::AddressDevice => TrbKind::from(unsafe { &mut *(value as *mut AddressDevice) }),
TrbType::ConfigureEndpoint => {
TrbKind::from(unsafe { &mut *(value as *mut ConfigureEndpoint) })
}
TrbType::EvaluateContext => {
TrbKind::from(unsafe { &mut *(value as *mut EvaluateContext) })
}
TrbType::ResetEndpoint => TrbKind::from(unsafe { &mut *(value as *mut ResetEndpoint) }),
TrbType::StopEndpoint => TrbKind::from(unsafe { &mut *(value as *mut StopEndpoint) }),
TrbType::SetTrDequeuePointer => {
TrbKind::from(unsafe { &mut *(value as *mut SetTrDequeuePointer) })
}
TrbType::ResetDevice => TrbKind::from(unsafe { &mut *(value as *mut ResetDevice) }),
TrbType::ForceEvent => TrbKind::from(unsafe { &mut *(value as *mut ForceEvent) }),
TrbType::NegotiateBandwidth => {
TrbKind::from(unsafe { &mut *(value as *mut NegotiateBandwidth) })
}
TrbType::SetLatencyToleranceValue => {
TrbKind::from(unsafe { &mut *(value as *mut SetLatencyToleranceValue) })
}
TrbType::GetPortBandwidth => {
TrbKind::from(unsafe { &mut *(value as *mut GetPortBandwidth) })
}
TrbType::ForceHeader => TrbKind::from(unsafe { &mut *(value as *mut ForceHeader) }),
TrbType::NoopCommand => TrbKind::from(unsafe { &mut *(value as *mut CmdNoop) }),
TrbType::GetExtendedProperty => {
TrbKind::from(unsafe { &mut *(value as *mut GetExtendedProperty) })
}
TrbType::SetExtendedProperty => {
TrbKind::from(unsafe { &mut *(value as *mut SetExtendedProperty) })
}
TrbType::TransferEvent => TrbKind::from(unsafe { &mut *(value as *mut TransferEvent) }),
TrbType::CommandCompletion => {
TrbKind::from(unsafe { &mut *(value as *mut CommandCompletion) })
}
TrbType::PortStatusChange => {
TrbKind::from(unsafe { &mut *(value as *mut PortStatusChange) })
}
TrbType::BandwidthRequest => {
TrbKind::from(unsafe { &mut *(value as *mut BandwidthRequest) })
}
TrbType::Doorbell => TrbKind::from(unsafe { &mut *(value as *mut Doorbell) }),
TrbType::HostController => {
TrbKind::from(unsafe { &mut *(value as *mut HostController) })
}
TrbType::DeviceNotification => {
TrbKind::from(unsafe { &mut *(value as *mut DeviceNotification) })
}
TrbType::MfindexWrap => TrbKind::from(unsafe { &mut *(value as *mut MfindexWrap) }),
}
}
}
impl<'a> Clone for CommandKind<'a> {
fn clone(&self) -> Self {
match self {
CommandKind::AddressDevice(_) => {
Self::AddressDevice(unsafe { &mut *(addralloc::<AddressDevice>()) })
}
CommandKind::ConfigureEndpoint(_) => {
Self::ConfigureEndpoint(unsafe { &mut *(addralloc::<ConfigureEndpoint>()) })
}
CommandKind::DisableSlot(_) => {
Self::DisableSlot(unsafe { &mut *(addralloc::<DisableSlot>()) })
}
CommandKind::EnableSlot(_) => {
Self::EnableSlot(unsafe { &mut *(addralloc::<EnableSlot>()) })
}
CommandKind::EvaluateContext(_) => {
Self::EvaluateContext(unsafe { &mut *(addralloc::<EvaluateContext>()) })
}
CommandKind::ForceEvent(_) => {
Self::ForceEvent(unsafe { &mut *(addralloc::<ForceEvent>()) })
}
CommandKind::ForceHeader(_) => {
Self::ForceHeader(unsafe { &mut *(addralloc::<ForceHeader>()) })
}
CommandKind::GetExtendedProperty(_) => {
Self::GetExtendedProperty(unsafe { &mut *(addralloc::<GetExtendedProperty>()) })
}
CommandKind::GetPortBandwidth(_) => {
Self::GetPortBandwidth(unsafe { &mut *(addralloc::<GetPortBandwidth>()) })
}
CommandKind::NegotiateBandwidth(_) => {
Self::NegotiateBandwidth(unsafe { &mut *(addralloc::<NegotiateBandwidth>()) })
}
CommandKind::CmdNoop(_) => Self::CmdNoop(unsafe { &mut *(addralloc::<CmdNoop>()) }),
CommandKind::ResetDevice(_) => {
Self::ResetDevice(unsafe { &mut *(addralloc::<ResetDevice>()) })
}
CommandKind::ResetEndpoint(_) => {
Self::ResetEndpoint(unsafe { &mut *(addralloc::<ResetEndpoint>()) })
}
CommandKind::SetExtendedProperty(_) => {
Self::SetExtendedProperty(unsafe { &mut *(addralloc::<SetExtendedProperty>()) })
}
CommandKind::SetLatencyToleranceValue(_) => Self::SetLatencyToleranceValue(unsafe {
&mut *(addralloc::<SetLatencyToleranceValue>())
}),
CommandKind::SetTrDequeuePointer(_) => {
Self::SetTrDequeuePointer(unsafe { &mut *(addralloc::<SetTrDequeuePointer>()) })
}
CommandKind::StopEndpoint(_) => {
Self::StopEndpoint(unsafe { &mut *(addralloc::<StopEndpoint>()) })
}
}
}
}
impl<'a> Clone for EventKind<'a> {
fn clone(&self) -> Self {
match self {
EventKind::BandwidthRequest(_) => {
Self::BandwidthRequest(unsafe { &mut *(addralloc::<BandwidthRequest>()) })
}
EventKind::CommandCompletion(_) => {
Self::CommandCompletion(unsafe { &mut *(addralloc::<CommandCompletion>()) })
}
EventKind::DeviceNotification(_) => {
Self::DeviceNotification(unsafe { &mut *(addralloc::<DeviceNotification>()) })
}
EventKind::Doorbell(_) => Self::Doorbell(unsafe { &mut *(addralloc::<Doorbell>()) }),
EventKind::HostController(_) => {
Self::HostController(unsafe { &mut *(addralloc::<HostController>()) })
}
EventKind::MfindexWrap(_) => {
Self::MfindexWrap(unsafe { &mut *(addralloc::<MfindexWrap>()) })
}
EventKind::PortStatusChange(_) => {
Self::PortStatusChange(unsafe { &mut *(addralloc::<PortStatusChange>()) })
}
EventKind::TransferEvent(_) => {
Self::TransferEvent(unsafe { &mut *(addralloc::<TransferEvent>()) })
}
}
}
}
impl<'a> Clone for TransferKind<'a> {
fn clone(&self) -> Self {
match self {
TransferKind::DataStage(_) => {
Self::DataStage(unsafe { &mut *(addralloc::<DataStage>()) })
}
TransferKind::EventData(_) => {
Self::EventData(unsafe { &mut *(addralloc::<EventData>()) })
}
TransferKind::Isoch(_) => Self::Isoch(unsafe { &mut *(addralloc::<Isoch>()) }),
TransferKind::TransferNoop(_) => {
Self::TransferNoop(unsafe { &mut *(addralloc::<TransferNoop>()) })
}
TransferKind::Normal(_) => Self::Normal(unsafe { &mut *(addralloc::<Normal>()) }),
TransferKind::SetupStage(_) => {
Self::SetupStage(unsafe { &mut *(addralloc::<SetupStage>()) })
}
TransferKind::StatusStage(_) => {
Self::StatusStage(unsafe { &mut *(addralloc::<StatusStage>()) })
}
}
}
}
impl<'a> Clone for TrbKind<'a> {
fn clone(&self) -> Self {
match self {
Self::Command(cmd) => Self::Command(cmd.clone()),
Self::Event(evt) => Self::Event(evt.clone()),
Self::Transfer(tr) => Self::Transfer(tr.clone()),
Self::Link(_) => Self::Link(unsafe { &mut *(addralloc::<Link>()) }),
}
}
}
// Needed for ensuring proper synchronization of indices
static CMD_RING_IDX: AtomicUsize = AtomicUsize::new(0);
static EVENT_RING_IDX: AtomicUsize = AtomicUsize::new(0);
pub struct XhciImpl {
regs: Option<Registers<XhciMapper>>,
extcaps: Option<List<XhciMapper>>,
cmd_ring: &'static mut [CommandKind<'static>],
event_ring_dequeue: &'static mut [EventKind<'static>],
transfer_ring: &'static mut [TransferKind<'static>],
}
impl XhciImpl {
pub fn new(header: &Header) -> Self {
let offset_full_bar_outer = OnceCell::<usize>::uninit();
let regs = {
if let DeviceKind::UsbController =
DeviceKind::new(header.class_code.base as u32, header.class_code.sub as u32)
{
if let HeaderType::Normal(header) = header.header_type.clone() {
let bar0 = header.base_addresses.orig()[0];
let bar1 = header.base_addresses.orig()[1];
// Align this properly
let full_bar = bar0 as u64 | ((bar1 as u64) << 32);
let offset_full_bar = {
let test = Page::<Size4KiB>::containing_address(VirtAddr::new(full_bar));
test.start_address().as_u64()
} as usize;
offset_full_bar_outer.get_or_init(move || offset_full_bar);
let regs = unsafe {
Registers::new(
offset_full_bar_outer.get().cloned().unwrap(),
MAPPER.read().clone(),
)
};
Some(regs)
} else {
None
}
} else {
None
}
};
let extcaps = regs.as_ref().and_then(|regs| {
let extended_caps = unsafe {
List::new(
offset_full_bar_outer.get().cloned().unwrap(),
regs.capability.hccparams1.read_volatile(),
MAPPER.read().clone(),
)
};
extended_caps
});
Self {
regs,
extcaps,
cmd_ring: unsafe {
core::slice::from_raw_parts_mut::<'static>(
addralloc::<CommandKind<'_>>(),
Page::<Size4KiB>::SIZE as usize / core::mem::size_of::<CommandKind<'_>>(),
)
},
event_ring_dequeue: unsafe {
core::slice::from_raw_parts_mut::<'static>(
addralloc::<EventKind<'_>>(),
Page::<Size4KiB>::SIZE as usize / core::mem::size_of::<EventKind<'_>>(),
)
},
transfer_ring: unsafe {
core::slice::from_raw_parts_mut::<'static>(
addralloc::<TransferKind<'_>>(),
Page::<Size4KiB>::SIZE as usize / core::mem::size_of::<TransferKind<'_>>(),
)
},
}
}
pub fn capabilities_mut(&mut self) -> Option<&mut Capability<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe { &mut (*(regs as *mut Registers<XhciMapper>)).capability })
}
pub fn doorbell_mut(&mut self) -> Option<&mut ReadWrite<Register, XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe { &mut (*(regs as *mut Registers<XhciMapper>)).doorbell })
}
pub fn operational_mut(&mut self) -> Option<&mut Operational<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe { &mut (*(regs as *mut Registers<XhciMapper>)).operational })
}
pub fn port_register_set_mut(&mut self) -> Option<&mut ReadWrite<PortRegisterSet, XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe { &mut (*(regs as *mut Registers<XhciMapper>)).port_register_set })
}
pub fn runtime_mut(&mut self) -> Option<&mut Runtime<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe { &mut (*(regs as *mut Registers<XhciMapper>)).runtime })
}
pub fn interrupter_register_set_mut(
&mut self,
) -> Option<&mut InterrupterRegisterSet<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.regs) as *mut Option<Registers<XhciMapper>>) }
.as_mut()
.map(|regs| unsafe {
&mut (*(regs as *mut Registers<XhciMapper>)).interrupter_register_set
})
}
pub fn capabilities(&self) -> Option<&Capability<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe { &(*(regs as *const Registers<XhciMapper>)).capability })
}
pub fn doorbell(&self) -> Option<&ReadWrite<Register, XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe { &(*(regs as *const Registers<XhciMapper>)).doorbell })
}
pub fn operational(&self) -> Option<&Operational<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe { &(*(regs as *const Registers<XhciMapper>)).operational })
}
pub fn port_register_set(&self) -> Option<&ReadWrite<PortRegisterSet, XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe { &(*(regs as *const Registers<XhciMapper>)).port_register_set })
}
pub fn runtime(&self) -> Option<&Runtime<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe { &(*(regs as *const Registers<XhciMapper>)).runtime })
}
pub fn interrupter_register_set(&self) -> Option<&InterrupterRegisterSet<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &*((&self.regs) as *const Option<Registers<XhciMapper>>) }
.as_ref()
.map(|regs| unsafe {
&(*(regs as *const Registers<XhciMapper>)).interrupter_register_set
})
}
pub fn extcaps(&self) -> Option<&List<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { *(&(self.extcaps.as_ref()) as *const Option<&List<XhciMapper>>) }
}
pub fn extcaps_mut(&mut self) -> Option<&mut List<XhciMapper>> {
// Borrow checker throws a hissy fit when raw pointers aren't used here
unsafe { &mut *((&mut self.extcaps) as *mut Option<List<XhciMapper>>) }.as_mut()
}
pub fn init(&mut self) {
// borrow checker
let cmd_ring_addr = self.cmd_ring.first().unwrap() as *const CommandKind<'_> as u64;
let erdp_raw = self.event_ring_dequeue.first().unwrap() as *const EventKind<'_> as u64;
// will need this later
let _transfer_ring_ptr = self.transfer_ring as *mut [TransferKind<'_>];
if let Some(op) = self.operational_mut() {
log::info!("XHCI: Waiting for controller");
while op.usbsts.read_volatile().controller_not_ready() {
core::hint::spin_loop();
}
// Stop the host controller
op.usbcmd.update_volatile(|cmd| {
cmd.clear_run_stop();
});