This repository has been 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
/
Rings.cpp
142 lines (131 loc) · 4.08 KB
/
Rings.cpp
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
//
// Rings.cpp
// GenericUSBXHCI
//
// Created by Zenith432 on October 10th 2013.
// Copyright (c) 2013 Zenith432. All rights reserved.
//
//
#include "GenericUSBXHCI.h"
#define CLASS GenericUSBXHCI
#define super IOUSBControllerV3
#pragma mark -
#pragma mark Rings
#pragma mark -
__attribute__((visibility("hidden")))
IOReturn CLASS::AllocRing(ringStruct* pRing, int32_t numPages)
{
IOReturn rc = MakeBuffer(kIOMemoryPhysicallyContiguous | kIODirectionInOut,
numPages * PAGE_SIZE,
-PAGE_SIZE,
&pRing->md,
reinterpret_cast<void**>(&pRing->ptr),
&pRing->physAddr);
if (rc != kIOReturnSuccess)
return kIOReturnNoMemory;
pRing->numTRBs = static_cast<uint16_t>(numPages * (PAGE_SIZE / sizeof *pRing->ptr));
pRing->numPages = static_cast<uint16_t>(numPages);
pRing->cycleState = 1U;
InitPreallocedRing(pRing);
return kIOReturnSuccess;
}
__attribute__((visibility("hidden")))
void CLASS::InitPreallocedRing(ringStruct* pRing)
{
pRing->enqueueIndex = 0U;
pRing->dequeueIndex = 0U;
pRing->lastSeenDequeueIndex = 0U;
pRing->lastSeenFrame = 0U;
pRing->nextIsocFrame = 0ULL;
TRBStruct* t = &pRing->ptr[pRing->numTRBs - 1U];
SetTRBAddr64(t, pRing->physAddr);
t->d |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) | XHCI_TRB_3_TC_BIT;
}
__attribute__((visibility("hidden")))
ringStruct* CLASS::GetRing(int32_t slot, int32_t endpoint, uint32_t streamId)
{
if (streamId <= ConstSlotPtr(slot)->lastStreamForEndpoint[endpoint]) {
ringStruct* pRing = SlotPtr(slot)->ringArrayForEndpoint[endpoint];
if (pRing)
return &pRing[streamId];
}
return 0;
}
__attribute__((visibility("hidden")))
void CLASS::DeallocRing(ringStruct* pRing)
{
if (!pRing)
return;
if (pRing->md) {
pRing->md->complete();
pRing->md->release();
pRing->md = 0;
}
pRing->ptr = 0;
pRing->physAddr = 0ULL;
pRing->numTRBs = 0U;
}
__attribute__((visibility("hidden")))
ringStruct* CLASS::CreateRing(int32_t slot, int32_t endpoint, uint32_t maxStream)
{
SlotStruct* pSlot = SlotPtr(slot);
if (pSlot->ringArrayForEndpoint[endpoint]) {
if (maxStream > pSlot->maxStreamForEndpoint[endpoint])
return 0;
return pSlot->ringArrayForEndpoint[endpoint];
}
ringStruct* pRing = static_cast<ringStruct*>(IOMalloc((1U + maxStream) * sizeof *pRing));
if (!pRing)
return pRing;
bzero(pRing, (1U + maxStream) * sizeof *pRing);
pSlot->maxStreamForEndpoint[endpoint] = static_cast<uint16_t>(maxStream);
pSlot->lastStreamForEndpoint[endpoint] = 0U;
pSlot->ringArrayForEndpoint[endpoint] = pRing;
for (uint32_t streamId = 0U; streamId <= maxStream; ++streamId) {
pRing[streamId].slot = static_cast<uint8_t>(slot);
pRing[streamId].endpoint = static_cast<uint8_t>(endpoint);
}
return pRing;
}
__attribute__((visibility("hidden")))
int32_t CLASS::CountRingToED(ringStruct const* pRing, int32_t trbIndexInRingQueue, uint32_t* pShortFall)
{
int32_t next;
uint32_t trbType;
TRBStruct* pTrb = &pRing->ptr[trbIndexInRingQueue];
/*
* Note: CountRingToED is called with an index taken from
* a Transfer Event TRB. For a stop TRB the index
* may be pRing->enqueueIndex, so check to make sure.
*/
if (trbIndexInRingQueue == pRing->enqueueIndex)
return trbIndexInRingQueue;
trbType = XHCI_TRB_3_TYPE_GET(pTrb->d);
while (trbType != XHCI_TRB_TYPE_EVENT_DATA &&
(pTrb->d & XHCI_TRB_3_CHAIN_BIT)) {
next = trbIndexInRingQueue + 1;
if (trbType == XHCI_TRB_TYPE_LINK || next >= static_cast<int32_t>(pRing->numTRBs) - 1)
next = 0;
if (next == static_cast<int32_t>(pRing->enqueueIndex))
break;
trbIndexInRingQueue = next;
pTrb = &pRing->ptr[trbIndexInRingQueue];
trbType = XHCI_TRB_3_TYPE_GET(pTrb->d);
if (trbType == XHCI_TRB_TYPE_NORMAL)
*pShortFall += XHCI_TRB_2_BYTES_GET(pTrb->c);
}
return trbIndexInRingQueue;
}
__attribute__((visibility("hidden")))
uint16_t CLASS::NextTransferDQ(ringStruct const* pRing, int32_t index)
{
/*
* Note: Assumes index != pRing->enqueueIndex
*/
++index;
if ((index >= static_cast<int32_t>(pRing->numTRBs) - 1) ||
(index != pRing->enqueueIndex &&
XHCI_TRB_3_TYPE_GET(pRing->ptr[index].d) == XHCI_TRB_TYPE_LINK))
index = 0;
return static_cast<uint16_t>(index);
}