This repository was archived by the owner on Sep 5, 2023. It is now read-only.
forked from RehabMan/OS-X-Generic-USB3
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathV1Pure.cpp
More file actions
1088 lines (1052 loc) · 34.6 KB
/
V1Pure.cpp
File metadata and controls
1088 lines (1052 loc) · 34.6 KB
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
//
// V1Pure.cpp
// GenericUSBXHCI
//
// Created by Zenith432 on December 26th, 2012.
// Copyright (c) 2012-2014 Zenith432. All rights reserved.
//
#include "GenericUSBXHCI.h"
#include "Async.h"
#include "XHCITRB.h"
#include "XHCITypes.h"
#include <IOKit/IOFilterInterruptEventSource.h>
#include <IOKit/usb/IOUSBRootHubDevice.h>
#define CLASS GenericUSBXHCI
#define super IOUSBControllerV3
#pragma mark -
#pragma mark IOUSBController Pure
#pragma mark -
IOReturn CLASS::UIMInitialize(IOService* provider)
{
IOReturn rc;
_device = OSDynamicCast(IOPCIDevice, provider);
if (!_device) {
IOLog("%s: provider is not IOPCIDevice\n", __FUNCTION__);
return kIOReturnBadArgument;
}
_deviceBase = _device->mapDeviceMemoryWithIndex(0U);
if (!_deviceBase) {
IOLog("%s: mapDeviceMemoryWithIndex failed\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoResources;
}
SetVendorInfo();
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 || defined(REHABMAN_UNIVERSAL_BUILD)
if (CHECK_FOR_MAVERICKS) {
#ifdef REHABMAN_UNIVERSAL_BUILD
uint64_t errata64 = ((Mavericks_IOUSBControllerV3*)this)->GetErrata64Bits(_vendorID, _deviceID, _revisionID);
#else
uint64_t errata64 = super::GetErrata64Bits(_vendorID, _deviceID, _revisionID);
#endif
WRITE_V3EXPANSION(_errata64Bits, errata64);
if (errata64 & (1ULL << 34U)) {
/*
* TBD: Mavericks 15517 - 156C7
*/
}
}
#endif
_errataBits = GetErrataBits(_vendorID, _deviceID, _revisionID); // Note: originally |=
if (!ON_THUNDERBOLT)
_expansionData->_isochMaxBusStall = 25000U;
OverrideErrataFromProps();
_pXHCICapRegisters = reinterpret_cast<struct XHCICapRegisters volatile*>(_deviceBase->getVirtualAddress());
#if 0
if (m_invalid_regspace) {
UIMFinalize();
return kIOReturnNoDevice;
}
#endif
/*
* TBD: This also disables bus-mastering. Bios may still
* own the xHC. Is that a problem?
*/
// enable the card registers
_device->configWrite16(kIOPCIConfigCommand, kIOPCICommandMemorySpace);
#if 0
uint16_t hciv = Read16Reg(&_pXHCICapRegisters->HCIVersion);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (1)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
if (hciv < 0x100U) {
IOLog("%s: Unsupported xHC version %#x\n", __FUNCTION__, static_cast<uint32_t>(hciv));
UIMFinalize();
return kIOReturnUnsupported;
}
#endif
uint32_t hcp1 = Read32Reg(&_pXHCICapRegisters->HCSParams[0]);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (2)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
_maxInterrupters = static_cast<uint16_t>(XHCI_HCS1_IRQ_MAX(hcp1));
_numSlots = static_cast<uint8_t>(XHCI_HCS1_DEVSLOT_MAX(hcp1));
_rootHubNumPorts = static_cast<uint8_t>(XHCI_HCS1_N_PORTS(hcp1));
if (!_rootHubNumPorts || _rootHubNumPorts > kMaxRootPorts) {
IOLog("%s: Invalid number of root hub ports == %u\n", __FUNCTION__, _rootHubNumPorts);
UIMFinalize();
return kIOReturnDeviceError;
}
_filterInterruptSource = IOFilterInterruptEventSource::filterInterruptEventSource(this,
InterruptHandler,
PrimaryInterruptFilter,
_device,
findInterruptIndex(_device,
!(_errataBits & kErrataDisableMSI)));
if (!_filterInterruptSource) {
IOLog("%s: Unable to create filterInterruptEventSource\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoResources;
}
_baseInterruptIndex = _filterInterruptSource->getIntIndex();
rc = _workLoop->addEventSource(_filterInterruptSource);
if (rc != kIOReturnSuccess) {
IOLog("%s: Unable to add filter to workloop, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
rc = InitializeEventSource();
if (rc != kIOReturnSuccess) {
IOLog("%s: Unable to create private IOEventSource and add to workloop, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
uint32_t hcc = Read32Reg(&_pXHCICapRegisters->HCCParams);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (3)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
DecodeExtendedCapability(hcc);
TakeOwnershipFromBios();
_maxNumEndpoints = (kUSBMaxPipes - 1) * 256;
EnableXHCIPorts();
if (_errataBits & kErrataIntelPantherPoint)
_maxNumEndpoints = 64;
uint32_t u = Read8Reg(&_pXHCICapRegisters->CapLength);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (4)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
_pXHCIOperationalRegisters = reinterpret_cast<struct XHCIOpRegisters volatile*>(reinterpret_cast<uint8_t volatile*>(_pXHCICapRegisters) + u);
u = Read32Reg(&_pXHCICapRegisters->RTSOff);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (5)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
_pXHCIRuntimeRegisters = reinterpret_cast<struct XHCIRuntimeRegisters volatile*>(reinterpret_cast<uint8_t volatile*>(_pXHCICapRegisters) + u);
u = Read32Reg(&_pXHCICapRegisters->DBOff);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (6)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
_pXHCIDoorbellRegisters = reinterpret_cast<uint32_t volatile*>(reinterpret_cast<uint8_t volatile*>(_pXHCICapRegisters) + u);
DisableComplianceMode();
u = XHCI_HCC_PSA_SZ_MAX(hcc);
if (u)
_maxPSASize = 2U << u;
else
_maxPSASize = 0U;
_HCCLow = static_cast<uint8_t>(hcc & 255U);
_inputContext.refCount = 0;
rc = ResetController();
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (7)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
if (rc != kIOReturnSuccess) {
IOLog("%s: ResetController failed, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
rc = InitializePorts();
if (rc != kIOReturnSuccess) {
IOLog("%s: Invalid regspace (8)\n", __FUNCTION__);
UIMFinalize();
return rc;
}
_hub3Address = kXHCISSRootHubAddress;
_hub2Address = kXHCIUSB2RootHubAddress;
u = Read32Reg(&_pXHCIOperationalRegisters->Config);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (9)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
Write32Reg(&_pXHCIOperationalRegisters->Config, (u & ~XHCI_CONFIG_SLOTS_MASK) | _numSlots);
Write32Reg(&_pXHCIOperationalRegisters->DNCtrl, UINT16_MAX);
#if 0
if (_maxNumEndpoints < gXHCIEPlimit) {
_maxNumEndpoints = gXHCIEPlimit;
setProperty("Max XHCI EPs", gXHCIEPlimit, 16U);
}
#endif
_slotArray = static_cast<SlotStruct*>(IOMalloc(static_cast<size_t>(_numSlots) * sizeof *_slotArray));
if (!_slotArray) {
IOLog("%s: Failed to allocate memory for slots\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoMemory;
}
bzero(_slotArray, static_cast<size_t>(_numSlots) * sizeof *_slotArray);
rc = MakeBuffer(kIOMemoryPhysicallyContiguous | kIODirectionInOut,
(1U + static_cast<size_t>(_numSlots)) * sizeof *_dcbaa.ptr,
-PAGE_SIZE,
&_dcbaa.md,
reinterpret_cast<void**>(&_dcbaa.ptr),
&_dcbaa.physAddr);
if (rc != kIOReturnSuccess) {
IOLog("%s: MakeBuffer(1) failed, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
#if 0
bzero(_dcbaa.ptr, (1U + static_cast<size_t>(_numSlots)) * sizeof *_dcbaa.ptr);
#endif
Write64Reg(&_pXHCIOperationalRegisters->DCBAap, _dcbaa.physAddr, false);
_commandRing.numTRBs = PAGE_SIZE / sizeof *_commandRing.ptr;
_commandRing.callbacks = static_cast<TRBCallbackEntry*>(IOMalloc(static_cast<size_t>(_commandRing.numTRBs) * sizeof *_commandRing.callbacks));
if (!_commandRing.callbacks) {
IOLog("%s: IOMalloc failed\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoMemory;
}
bzero(_commandRing.callbacks, static_cast<size_t>(_commandRing.numTRBs) * sizeof *_commandRing.callbacks);
rc = MakeBuffer(kIOMemoryPhysicallyContiguous | kIODirectionInOut,
static_cast<size_t>(_commandRing.numTRBs) * sizeof *_commandRing.ptr,
-PAGE_SIZE,
&_commandRing.md,
reinterpret_cast<void**>(&_commandRing.ptr),
&_commandRing.physAddr);
if (rc != kIOReturnSuccess) {
IOLog("%s: MakeBuffer(2) failed, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
InitCMDRing();
uint32_t hcp2 = Read32Reg(&_pXHCICapRegisters->HCSParams[1]);
if (m_invalid_regspace) {
IOLog("%s: Invalid regspace (10)\n", __FUNCTION__);
UIMFinalize();
return kIOReturnNoDevice;
}
_istKeepAwayFrames = (hcp2 & 8U) ? (hcp2 & 7U) : 1U; // Note: minimum of 1 frame
setProperty("ISTKeepAway", _istKeepAwayFrames, 8U);
_erstMax = 1U << XHCI_HCS2_ERST_MAX(hcp2);
for (int32_t interrupter = 0; interrupter < kMaxActiveInterrupters; ++interrupter) {
rc = InitAnEventRing(interrupter);
if (rc != kIOReturnSuccess) {
IOLog("%s: InitAnEventRing(%d) failed, error == %#x\n", __FUNCTION__, interrupter, rc);
UIMFinalize();
return rc;
}
}
bzero(const_cast<int32_t*>(&_errorCounters[0]), sizeof _errorCounters);
rc = MakeBuffer(kIOMemoryPhysicallyContiguous | kIODirectionInOut,
GetInputContextSize(),
-PAGE_SIZE,
&_inputContext.md,
reinterpret_cast<void**>(&_inputContext.ptr),
&_inputContext.physAddr);
if (rc != kIOReturnSuccess) {
IOLog("%s: MakeBuffer(3) failed, error == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
_scratchpadBuffers.max = static_cast<uint8_t>(XHCI_HCS2_SPB_MAX(hcp2));
#if 0
_maxScratchpadBuffers |= ((hcp2 >> 16) & 0x3E0U); // Note: These are listed as reserved in xHCI 1.0 Spec
#endif
rc = AllocScratchpadBuffers();
if (rc != kIOReturnSuccess) {
UIMFinalize();
return rc;
}
/*
* Note: Mavericks extra Makebuffer for use in ExecuteGetPortBandwidthWorkaround()
*/
bzero(&_addressMapper, sizeof _addressMapper);
RHPortStatusChangeBitmapInit();
_isSleeping = false;
_deviceZero.isBeingAddressed = false;
#if 0
_filterInterruptActive = false;
#endif
_millsecondCounter = 0ULL;
bzero(&_interruptCounters[0], sizeof _interruptCounters);
_HSEDetected = false;
_RenesasControllerVersion = 0U;
_debugCtr = 0U;
_debugPattern = 0xDEADBEEFU;
/*
* TBD: Mavericks 166FE - 16782
*/
rc = AllocRHThreadCalls();
if (rc != kIOReturnSuccess) {
IOLog("%s: AllocRHThreadCalls failed, rc == %#x\n", __FUNCTION__, rc);
UIMFinalize();
return rc;
}
CheckSleepCapability();
/*
* TBD: Mavericks 167EC - 169D3
* some code to create diagnostics
* more stuff
*/
SetPropsForBookkeeping();
_completer.setOwner(this);
_uimInitialized = true;
registerService();
return kIOReturnSuccess;
}
IOReturn CLASS::UIMFinalize(void)
{
_completer.Finalize();
if (_providerACPIDevice) {
_providerACPIDevice->release();
_providerACPIDevice = 0;
}
if (_slotArray) {
for (uint8_t slot = 1U; slot <= _numSlots; ++slot)
NukeSlot(slot);
IOFree(_slotArray, static_cast<size_t>(_numSlots) * sizeof *_slotArray);
_slotArray = 0;
}
if (_dcbaa.md) {
_dcbaa.md->complete();
_dcbaa.md->release();
_dcbaa.md = 0;
}
if (_commandRing.callbacks) {
IOFree(_commandRing.callbacks, _commandRing.numTRBs * sizeof *_commandRing.callbacks);
_commandRing.callbacks = 0;
}
if (_commandRing.md) {
_commandRing.md->complete();
_commandRing.md->release();
_commandRing.md = 0;
}
for (int32_t interrupter = 0; interrupter < kMaxActiveInterrupters; ++interrupter)
FinalizeAnEventRing(interrupter);
if (_inputContext.md) {
_inputContext.md->complete();
_inputContext.md->release();
_inputContext.md = 0;
}
FinalizeScratchpadBuffers();
FinalizeEventSource();
if (_filterInterruptSource && _workLoop) {
_workLoop->removeEventSource(_filterInterruptSource);
_filterInterruptSource->release();
_filterInterruptSource = 0;
}
if (_deviceBase) {
_deviceBase->release();
_deviceBase = 0;
}
FinalizeRHThreadCalls();
/*
* Note: Mavericks releases extra buffer used by ExecuteGetPortBandwidthWorkaround()
* and XHCIDiagnostics object.
*/
_uimInitialized = false;
return kIOReturnSuccess;
}
IOReturn CLASS::UIMAbortEndpoint(short functionNumber, short endpointNumber, short direction)
{
return UIMAbortStream(kUSBAllStreams, functionNumber, endpointNumber, direction);
}
IOReturn CLASS::UIMDeleteEndpoint(short functionNumber, short endpointNumber, short direction)
{
uint8_t slot, endpoint;
ringStruct* pRing;
SlotStruct* pSlot;
slot = GetSlotID(functionNumber);
if (!slot)
return functionNumber ? kIOReturnBadArgument : kIOReturnSuccess;
pSlot = SlotPtr(slot);
if (endpointNumber) {
endpoint = TranslateEndpoint(endpointNumber, direction);
if (!endpoint || endpoint >= kUSBMaxPipes)
return kIOReturnBadArgument;
} else
endpoint = 1U;
pRing = pSlot->ringArrayForEndpoint[endpoint];
if (pRing)
pRing->deleteInProgress = true;
/*
* Note: Always park a control endpoint because it's
* going to be deallocated without prior deconfigure.
* Never park a streams endpoint.
* Rest of endpoints - according to errata.
*
* Mavericks checks _controllerAvailable before doing this.
*/
DeconfigureEndpoint(slot, endpoint, !endpointNumber || ((_errataBits & kErrataParkRing) && !pSlot->IsStreamsEndpoint(endpoint)));
if (pRing) {
if (pSlot->IsStreamsEndpoint(endpoint))
DeleteStreams(slot, endpoint);
if ((pRing->epType | CTRL_EP) == ISOC_IN_EP) {
if (pRing->isochEndpoint) {
DeleteIsochEP(pRing->isochEndpoint);
static_cast<void>(__sync_fetch_and_sub(&_numEndpoints, 1));
pRing->isochEndpoint = 0;
}
} else {
if (pRing->asyncEndpoint) {
pRing->asyncEndpoint->Abort();
pRing->asyncEndpoint->release();
static_cast<void>(__sync_fetch_and_sub(&_numEndpoints, 1));
pRing->asyncEndpoint = 0;
}
}
DeallocRing(pRing);
IOFree(pRing, (1U + static_cast<size_t>(pSlot->maxStreamForEndpoint[endpoint])) * sizeof *pRing);
pSlot->maxStreamForEndpoint[endpoint] = 0U;
pSlot->lastStreamForEndpoint[endpoint] = 0U;
pSlot->ringArrayForEndpoint[endpoint] = 0;
}
for (endpoint = 1U; endpoint != kUSBMaxPipes; ++endpoint) {
pRing = pSlot->ringArrayForEndpoint[endpoint];
if (!pRing->isInactive())
return kIOReturnSuccess;
}
#if 0
/*
* Note: Mavericks
*/
if (!_controllerAvailable)
goto _Skip_CleanupControlEndpoint_And_IntelSWSlot;
#endif
CleanupControlEndpoint(slot, true);
#if 0
/*
* Note: Mavericks
*/
_IntelSWSlot = slot;
#endif
pSlot->ctx = 0;
SetDCBAAAddr64(&_dcbaa.ptr[slot], 0ULL);
if (pSlot->md) {
pSlot->md->complete();
pSlot->md->release();
pSlot->md = 0;
}
pSlot->physAddr = 0U;
pSlot->deviceNeedsReset = false;
_addressMapper.HubAddress[functionNumber] = 0U;
_addressMapper.PortOnHub[functionNumber] = 0U;
_addressMapper.Slot[functionNumber] = 0U;
_addressMapper.Active[functionNumber] = false;
return kIOReturnSuccess;
}
IOReturn CLASS::UIMClearEndpointStall(short functionNumber, short endpointNumber, short direction)
{
uint8_t slot, endpoint;
ringStruct* pRing;
ContextStruct* pContext;
slot = GetSlotID(functionNumber);
if (!slot)
return kIOReturnInternalError;
endpoint = TranslateEndpoint(endpointNumber, direction);
if (!endpoint || endpoint >= kUSBMaxPipes)
return kIOReturnBadArgument;
pRing = GetRing(slot, endpoint, 0U);
if (pRing->isInactive())
return kIOReturnBadArgument;
if (pRing->returnInProgress)
return kIOUSBClearPipeStallNotRecursive;
pContext = GetSlotContext(slot, endpoint);
if (XHCI_EPCTX_0_EPSTATE_GET(pContext->_e.dwEpCtx0) != EP_STATE_HALTED)
ClearEndpoint(slot, endpoint);
return UIMAbortStream(kUSBAllStreams, functionNumber, endpointNumber, direction);
}
void CLASS::UIMRootHubStatusChange(void)
{
#if 0
uint16_t statusChangedBitmap = 0U;
IOUSBHubStatus hubStatus;
IOUSBHubPortStatus portStatus;
uint32_t statusBit = 1U, portToCheck;
uint32_t sts = Read32Reg(&_pXHCIOperationalRegisters->USBSts);
if (m_invalid_regspace)
return;
if ((sts & XHCI_STS_HSE) && !_HSEDetected) {
IOLog("%s: HSE bit set:%#x (1)\n", __FUNCTION__, sts);
_HSEDetected = true;
}
if (!_controllerAvailable || _wakingFromHibernation)
return;
RHCheckForPortResumes();
if (GetRootHubStatus(&hubStatus) != kIOReturnSuccess) {
_rootHubStatusChangedBitmap = statusChangedBitmap;
return;
}
if (USBToHostWord(hubStatus.statusFlags) & (kHubLocalPowerStatus | kHubOverCurrentIndicator))
statusChangedBitmap |= statusBit;
statusBit <<= 1;
for (uint8_t port = 0U; port < _rootHubNumPorts; ++port, statusBit <<= 1) {
if (_rhPortResetPending[port]) {
if (CHECK_FOR_MAVERICKS)
RootHubStartTimer32(kUSBRootHubPollingRate);
continue;
}
portStatus.statusFlags = 0U;
portStatus.changeFlags = 0U;
portToCheck = PortNumberCanonicalToProtocol(port, reinterpret_cast<uint8_t*>(&portStatus.statusFlags));
if (!portToCheck)
continue;
if (GetRootHubPortStatus(&portStatus, portToCheck) != kIOReturnSuccess)
continue;
portStatus.changeFlags = USBToHostWord(portStatus.changeFlags);
portStatus.statusFlags = USBToHostWord(portStatus.statusFlags);
if ((portStatus.statusFlags & kHubPortConnection) &&
!(portStatus.changeFlags & kHubPortConnection) &&
_rhPortEmulateCSC[port])
portStatus.changeFlags |= kHubPortConnection;
if (!(portStatus.changeFlags & kHubPortSuperSpeedStateChangeMask))
continue;
portStatus.changeFlags |= kHubPortConnection;
statusChangedBitmap |= statusBit;
}
#else
uint32_t statusChangedBitmap = RHPortStatusChangeBitmapGrab();
if (gux_log_level >= 2 && statusChangedBitmap)
IOLog("%s: statusChangedBitmap == %#x\n", __FUNCTION__, statusChangedBitmap);
statusChangedBitmap |= _rhPortStatusChangeBitmapGated;
_rhPortStatusChangeBitmapGated = statusChangedBitmap;
if (!_controllerAvailable || _wakingFromHibernation)
statusChangedBitmap = 0U;
#endif
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 || defined(REHABMAN_UNIVERSAL_BUILD)
if (CHECK_FOR_MAVERICKS) {
if (_errataBits & kErrataVMwarePortSwap)
statusChangedBitmap = VMwarePortStatusShuffle(statusChangedBitmap, READ_V3EXPANSION(_rootHubNumPortsSS));
WRITE_V3EXPANSION(_rootHubStatusChangedBitmapSS, statusChangedBitmap);
return;
}
#endif
if (static_cast<uint16_t>(statusChangedBitmap >> 16))
RHClearUnserviceablePorts();
if (_errataBits & kErrataVMwarePortSwap)
statusChangedBitmap = VMwarePortStatusShuffle(statusChangedBitmap, READ_V3EXPANSION(_rootHubNumPortsSS));
_rootHubStatusChangedBitmap = static_cast<uint16_t>(statusChangedBitmap);
}
IOReturn CLASS::GetRootHubDeviceDescriptor(IOUSBDeviceDescriptor* desc)
{
static
IOUSBDeviceDescriptor const newDesc2 =
{
sizeof(IOUSBDeviceDescriptor), // UInt8 length;
kUSBDeviceDesc, // UInt8 descType;
HostToUSBWord(kUSBRel20), // UInt16 usbRel Supports USB 2.0;
kUSBHubClass, // UInt8 class;
kUSBHubSubClass, // UInt8 subClass;
1, // UInt8 protocol;
9, // UInt8 maxPacketSize;
HostToUSBWord(kAppleVendorID), // UInt16 vendor: Use the Apple Vendor ID from USB-IF
HostToUSBWord(kPrdRootHubAppleSS), // UInt16 product: All our root hubs are the same
HostToUSBWord(kUSBRel30), // UInt16 devRel:
2, // UInt8 manuIdx;
1, // UInt8 prodIdx;
0, // UInt8 serialIdx;
1 // UInt8 numConf;
};
static
IOUSBDeviceDescriptor const newDesc3 =
{
sizeof(IOUSBDeviceDescriptor), // UInt8 length;
kUSBDeviceDesc, // UInt8 descType;
HostToUSBWord(kUSBRel30), // UInt16 usbRel Supports USB 3.0;
kUSBHubClass, // UInt8 class;
kUSBHubSubClass, // UInt8 subClass;
kHubSuperSpeedProtocol, // UInt8 protocol;
9, // UInt8 maxPacketSize;
HostToUSBWord(kAppleVendorID), // UInt16 vendor: Use the Apple Vendor ID from USB-IF
HostToUSBWord(kPrdRootHubAppleSS), // UInt16 product: All our root hubs are the same
HostToUSBWord(kUSBRel30), // UInt16 devRel:
2, // UInt8 manuIdx;
1, // UInt8 prodIdx;
0, // UInt8 serialIdx;
1 // UInt8 numConf;
};
if (!desc)
return kIOReturnNoMemory;
if (((desc->bLength & kUSBSpeed_Mask) >> kUSBSpeed_Shift) != kUSBDeviceSpeedHigh)
bcopy(&newDesc3, desc, newDesc3.bLength);
else
bcopy(&newDesc2, desc, newDesc2.bLength);
return kIOReturnSuccess;
}
IOReturn CLASS::GetRootHubDescriptor(IOUSBHubDescriptor* desc)
{
IOUSBHubDescriptor hubDesc;
uint32_t appleCaptive, i, numBytes;
uint8_t* dstPtr;
OSNumber* appleCaptiveProperty;
OSObject* obj;
hubDesc.length = sizeof hubDesc;
hubDesc.hubType = kUSBHubDescriptorType;
hubDesc.numPorts = READ_V3EXPANSION(_rootHubNumPortsHS);
hubDesc.characteristics = HostToUSBWord(static_cast<uint16_t>(XHCI_HCC_PPC(_HCCLow) ? kPerPortSwitchingBit : 0U));
hubDesc.powerOnToGood = 250U;
hubDesc.hubCurrent = 0U;
numBytes = ((hubDesc.numPorts + 1U) / 8U) + 1U;
obj = _device->getProperty(kAppleInternalUSBDevice);
appleCaptive = 0U;
if (obj) {
appleCaptiveProperty = OSDynamicCast(OSNumber, obj);
if (appleCaptiveProperty)
appleCaptive = appleCaptiveProperty->unsigned32BitValue();
} else if (CHECK_FOR_MAVERICKS)
appleCaptive = CheckACPITablesForCaptiveRootHubPorts(_rootHubNumPorts);
if (appleCaptive) {
if (READ_V3EXPANSION(_rootHubPortsHSStartRange) > 1U)
appleCaptive >>= (READ_V3EXPANSION(_rootHubPortsHSStartRange) - 1U);
appleCaptive &= (2U << READ_V3EXPANSION(_rootHubNumPortsHS)) - 2U;
}
dstPtr = &hubDesc.removablePortFlags[0];
for (i = 0U; i < numBytes; i++) {
*dstPtr++ = static_cast<uint8_t>(appleCaptive & 0xFFU);
appleCaptive >>= 8;
}
for (i = 0U; i < numBytes; i++)
*dstPtr++ = 0xFFU;
hubDesc.length -= (((sizeof hubDesc.removablePortFlags) - numBytes) +
((sizeof hubDesc.pwrCtlPortFlags) - numBytes));
if (!desc)
return kIOReturnNoMemory;
bcopy(&hubDesc, desc, hubDesc.length);
return kIOReturnSuccess;
}
IOReturn CLASS::GetRootHubConfDescriptor(OSData* desc)
{
static
struct {
IOUSBConfigurationDescriptor confDesc;
IOUSBInterfaceDescriptor intfDesc;
IOUSBEndpointDescriptor endptDesc;
IOUSBSuperSpeedEndpointCompanionDescriptor compantionDesc;
} __attribute__((packed)) const allDesc = {
{
sizeof(IOUSBConfigurationDescriptor),//UInt8 length;
kUSBConfDesc, //UInt8 descriptorType;
HostToUSBWord(sizeof(IOUSBConfigurationDescriptor) +
sizeof(IOUSBInterfaceDescriptor) +
sizeof(IOUSBEndpointDescriptor) +
sizeof(IOUSBSuperSpeedEndpointCompanionDescriptor)), //UInt16 totalLength;
1, //UInt8 numInterfaces;
1, //UInt8 configValue;
0, //UInt8 configStrIndex;
0x60, //UInt8 attributes; self powered, supports remote wkup
0, //UInt8 maxPower;
},
{
sizeof(IOUSBInterfaceDescriptor),//UInt8 length;
kUSBInterfaceDesc, //UInt8 descriptorType;
0, //UInt8 interfaceNumber;
0, //UInt8 alternateSetting;
1, //UInt8 numEndpoints;
kUSBHubClass, //UInt8 interfaceClass;
kUSBHubSubClass, //UInt8 interfaceSubClass;
0, //UInt8 interfaceProtocol;
0 //UInt8 interfaceStrIndex;
},
{
sizeof(IOUSBEndpointDescriptor), // UInt8 length;
kUSBEndpointDesc, // UInt8 descriptorType;
0x81, // UInt8 endpointAddress; In, 1
0x10 | kUSBInterrupt, // UInt8 attributes;
HostToUSBWord(2), // UInt16 maxPacketSize;
9, // UInt8 interval (256 microframes or 32 ms)
},
{
sizeof(IOUSBSuperSpeedEndpointCompanionDescriptor),
kUSBSuperSpeedEndpointCompanion,
0,
0,
HostToUSBWord(2)
}
};
if (!desc || !desc->appendBytes(&allDesc, sizeof allDesc))
return(kIOReturnNoMemory);
return kIOReturnSuccess;
}
IOReturn CLASS::GetRootHubStatus(IOUSBHubStatus* pStatus)
{
if (pStatus)
*(uint32_t*) pStatus = 0U;
return kIOReturnSuccess;
}
IOReturn CLASS::GetRootHubPortStatus(IOUSBHubPortStatus* pStatus, UInt16 port)
{
uint32_t portSC;
uint16_t _port, statusFlags, changeFlags, linkState;
uint8_t protocol, speed;
bool didChange;
int32_t retries = 0;
if (!pStatus)
return kIOReturnBadArgument;
if (_myBusState < kUSBBusStateRunning)
return kIOReturnNotResponding;
if (m_invalid_regspace)
return kIOReturnNoDevice;
if (!_controllerAvailable)
return kIOReturnOffline;
/*
* Note: "protocol" is actually the speed of the requesting IOUSBRootHubDevice
* which is either kUSBDeviceSpeedHigh (2) or kUSBDeviceSpeedSuper (3).
* See IOUSBRootHubDevice::DeviceRequestWorker
*/
protocol = static_cast<uint8_t>((pStatus->statusFlags & kUSBSpeed_Mask) >> kUSBSpeed_Shift);
_port = PortNumberProtocolToCanonical(port, protocol);
if (_port >= _rootHubNumPorts)
return kIOReturnBadArgument;
retry:
portSC = Read32Reg(&_pXHCIOperationalRegisters->prs[_port].PortSC);
if (m_invalid_regspace)
return kIOReturnNoDevice;
++retries;
didChange = (portSC & XHCI_PS_CHANGEBITS) != 0U;
if (RHCheckForPortResume(_port, protocol, portSC))
portSC &= ~XHCI_PS_PLC;
/*
* Note overlap with
* kHubPortBeingReset | kHubPortOverCurrent | kHubPortEnabled | kHubPortConnection
*/
statusFlags = static_cast<uint16_t>(portSC & (XHCI_PS_PR | XHCI_PS_OCA | XHCI_PS_PED | XHCI_PS_CCS));
linkState = static_cast<uint16_t>(XHCI_PS_PLS_GET(portSC));
if (protocol != kUSBDeviceSpeedSuper) {
if (linkState == XDEV_U3 || linkState == XDEV_RESUME)
statusFlags |= kHubPortSuspend;
speed = static_cast<uint8_t>(XHCI_PS_SPEED_GET(portSC));
/*
* Note: Full Speed is default for USB2 port
*/
if (speed == XDEV_HS)
statusFlags |= kHubPortHighSpeed;
else if (speed == XDEV_LS)
statusFlags |= kHubPortLowSpeed;
if (portSC & XHCI_PS_PP)
statusFlags |= kHubPortPower;
/*
* Note: kHubPortTestMode may be set by reading PortPMSC
*/
if (XHCI_HCC_PIND(_HCCLow))
statusFlags |= kHubPortIndicator;
/*
* Extract PRC, OCC, PEC, CSC in correspondence with
* PR, OCA, PED, CCS above.
*/
changeFlags = static_cast<uint16_t>((portSC >> 17) & 0x1BU);
if (portSC & XHCI_PS_PLC)
changeFlags |= kHubPortSuspend;
if (portSC & (XHCI_PS_WRC | XHCI_PS_CEC))
/*
* Clear change bits a USB 2.0 port is not allowed to set
*/
Write32Reg(&_pXHCIOperationalRegisters->prs[_port].PortSC, (portSC & XHCI_PS_WRITEBACK_MASK) | XHCI_PS_WRC | XHCI_PS_CEC);
} else {
statusFlags |= ((linkState << kSSHubPortStatusLinkStateShift) & kSSHubPortStatusLinkStateMask);
if (portSC & XHCI_PS_PP)
statusFlags |= kSSHubPortStatusPowerMask;
#if 0
/*
* This is implied, as it is in fact zero
*/
statusFlags |= ((kSSHubPortSpeed5Gbps << kSSHubPortStatusSpeedShift) & kSSHubPortStatusSpeedMask);
#endif
changeFlags = static_cast<uint16_t>(((portSC >> 17) & 0x19U) | // PRC, OCC, CSC as above
((portSC >> 16) & 0xC0U) | // CEC, PLC as above
((portSC >> 14) & kSSHubPortChangeBHResetMask)); // WRC
if (portSC & XHCI_PS_PEC)
/*
* Clear change bit a superspeed port is not allowed to set
*/
Write32Reg(&_pXHCIOperationalRegisters->prs[_port].PortSC, (portSC & XHCI_PS_WRITEBACK_MASK) | XHCI_PS_PEC);
}
#ifdef DEBOUNCING
if (_rhPortBeingWarmReset[_port]) {
pStatus->statusFlags = HostToUSBWord(statusFlags | kHubPortConnection);
pStatus->changeFlags = HostToUSBWord(0U);
return kIOReturnSuccess;
}
#endif
if ((portSC & (XHCI_PS_CSC | XHCI_PS_CCS)) == XHCI_PS_CCS &&
_rhPortEmulateCSC[_port])
changeFlags |= kHubPortConnection;
#ifdef DEBOUNCING
HandlePortDebouncing(&statusFlags, &changeFlags, _port, linkState, protocol);
#endif
if (!changeFlags) {
if (didChange) {
if (retries < 5)
goto retry;
else {
IOLog("%s: Change bits on port %u, bus %#x misbehaving: portSC(%#x)\n",
__FUNCTION__, 1U + _port, static_cast<uint32_t>(_busNumber), portSC);
Write32Reg(&_pXHCIOperationalRegisters->prs[_port].PortSC, (portSC & XHCI_PS_WRITEBACK_MASK) | XHCI_PS_CHANGEBITS);
}
}
_rhPortStatusChangeBitmapGated &= ~(2U << _port);
}
pStatus->statusFlags = HostToUSBWord(statusFlags);
pStatus->changeFlags = HostToUSBWord(changeFlags);
/*
* Note: Mavericks B3A6 - B407
* Has some code to zero a counter (referenced in DoSoftRetries).
*/
return kIOReturnSuccess;
}
IOReturn CLASS::SetRootHubPortFeature(UInt16 wValue, UInt16 port)
{
uint16_t _port;
uint8_t protocol;
if (!_controllerAvailable)
return kIOReturnOffline;
protocol = static_cast<uint8_t>((wValue & kUSBSpeed_Mask) >> kUSBSpeed_Shift);
wValue = (wValue & kUSBAddress_Mask) >> kUSBAddress_Shift;
_port = PortNumberProtocolToCanonical(port & UINT8_MAX, protocol);
if (_port >= _rootHubNumPorts)
return kIOReturnBadArgument;
if (gux_log_level >= 2)
IOLog("%s(wValue %u, port %u)\n", __FUNCTION__, wValue, 1U + _port);
switch (wValue) {
case kUSBHubPortEnableFeature:
return XHCIRootHubEnablePort(_port, true);
case kUSBHubPortSuspendFeature:
return XHCIRootHubSuspendPort(protocol, _port, true);
case kUSBHubPortResetFeature:
return XHCIRootHubResetPort(protocol, _port);
case kUSBHubPortLinkStateFeature:
if (protocol != kUSBDeviceSpeedSuper)
return kIOReturnUnsupported;
return XHCIRootHubSetLinkStatePort(static_cast<uint8_t>(port >> 8), _port);
case kUSBHubPortPowerFeature:
XHCIRootHubPowerPort(_port, true);
break;
case kUSBHubPortBHPortResetFeature:
if (protocol != kUSBDeviceSpeedSuper)
return kIOReturnUnsupported;
return XHCIRootHubWarmResetPort(_port);
default:
return kIOReturnUnsupported;
}
return kIOReturnSuccess;
}
IOReturn CLASS::ClearRootHubPortFeature(UInt16 wValue, UInt16 port)
{
uint16_t _port;
uint8_t protocol;
if (!_controllerAvailable)
return kIOReturnOffline;
protocol = static_cast<uint8_t>((wValue & kUSBSpeed_Mask) >> kUSBSpeed_Shift);
wValue = (wValue & kUSBAddress_Mask) >> kUSBAddress_Shift;
_port = PortNumberProtocolToCanonical(port, protocol);
if (_port >= _rootHubNumPorts)
return kIOReturnBadArgument;
if (gux_log_level >= 2)
IOLog("%s(wValue %u, port %u)\n", __FUNCTION__, wValue, 1U + _port);
switch (wValue) {
case kUSBHubPortEnableFeature:
return XHCIRootHubEnablePort(_port, false);
case kUSBHubPortSuspendFeature:
return XHCIRootHubSuspendPort(protocol, _port, false);
case kUSBHubPortPowerFeature:
return XHCIRootHubPowerPort(_port, false);
case kUSBHubPortConnectionChangeFeature:
return XHCIRootHubClearPortConnectionChange(_port);
case kUSBHubPortEnableChangeFeature:
/*
* Shouldn't happen if protocol == kUSBDeviceSpeedSuper
*/
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_PEC);
case kUSBHubPortSuspendChangeFeature:
/*
* Shouldn't happen if protocol == kUSBDeviceSpeedSuper
* For USB2 port, maps to PLC
*/
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_PLC);
case kUSBHubPortOverCurrentChangeFeature:
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_OCC);
case kUSBHubPortResetChangeFeature:
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_PRC);
case kUSBHubPortLinkStateChangeFeature:
/*
* Shouldn't happen if protocol != kUSBDeviceSpeedSuper
*/
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_PLC);
case kUSBHubPortConfigErrorChangeFeature:
/*
* Shouldn't happen if protocol != kUSBDeviceSpeedSuper
*/
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_CEC);
case kUSBHubPortBHResetChangeFeature:
/*
* Shouldn't happen if protocol != kUSBDeviceSpeedSuper
*/
return XHCIRootHubClearPortChangeBit(_port, XHCI_PS_WRC);
default:
return kIOReturnUnsupported;
}
return kIOReturnSuccess;
}
IOReturn CLASS::SetHubAddress(UInt16 wValue)
{
if (((wValue & kUSBSpeed_Mask) >> kUSBSpeed_Shift) == kUSBDeviceSpeedSuper)
_hub3Address = (wValue & kUSBAddress_Mask) >> kUSBAddress_Shift;
else
_hub2Address = (wValue & kUSBAddress_Mask) >> kUSBAddress_Shift;
return kIOReturnSuccess;
}
UInt64 CLASS::GetFrameNumber(void)
{
return GetMicroFrameNumber() >> 3;
}
UInt32 CLASS::GetFrameNumber32(void)
{
return static_cast<uint32_t>(GetFrameNumber());
}
void CLASS::PollInterrupts(IOUSBCompletionAction safeAction)
{
uint32_t sts;
sts = Read32Reg(&_pXHCIOperationalRegisters->USBSts);
if (m_invalid_regspace)
return;
if (sts & XHCI_STS_PCD) {
Write32Reg(&_pXHCIOperationalRegisters->USBSts, XHCI_STS_PCD);
EnsureUsability();
if (_myPowerState == kUSBPowerStateOn) {
if (CHECK_FOR_MAVERICKS && _rhPortStatusChangeBitmap)
RootHubStartTimer32(kUSBRootHubPollingRate);
/*
* Note:
* RHCheckForPortResumes may be limited to the ports flagged
* by _rhPortStatusChangeBitmap | _rhPortStatusChangeBitmapGated
*/
RHCheckForPortResumes();
}
}
for (int32_t interrupter = 0; interrupter < kMaxActiveInterrupters; ++interrupter)
while (PollEventRing2(interrupter));
}
IOReturn CLASS::GetRootHubStringDescriptor(UInt8 index, OSData* desc)
{
// The following strings are in Unicode format
//
UInt8 productName3[] = {
0, // Length Byte
kUSBStringDesc, // Descriptor type
0x58, 0x00, // "X"
0x48, 0x00, // "H"
0x43, 0x00, // "C"
0x49, 0x00, // "I"
0x20, 0x00, // " "
0x52, 0x00, // "R"
0x6F, 0x00, // "o"
0x6f, 0x00, // "o"
0x74, 0x00, // "t"
0x20, 0x00, // " "
0x48, 0x00, // "H"
0x75, 0x00, // "u"
0x62, 0x00, // "b"
0x20, 0x00, // " "
0x53, 0x00, // "S"
0x53, 0x00, // "S"
0x20, 0x00, // " "
0x53, 0x00, // "S"
0x69, 0x00, // "i"
0x6d, 0x00, // "m"
0x75, 0x00, // "u"
0x6c, 0x00, // "l"
0x61, 0x00, // "a"