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
/
PwrMgmt.cpp
290 lines (272 loc) · 8.86 KB
/
PwrMgmt.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
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
//
// PwrMgmt.cpp
// GenericUSBXHCI
//
// Created by Zenith432 on January 5th 2013.
// Copyright (c) 2013-2014 Zenith432. All rights reserved.
//
#include "GenericUSBXHCI.h"
#define CLASS GenericUSBXHCI
#define super IOUSBControllerV3
#pragma mark -
#pragma mark Power Management
#pragma mark -
__attribute__((visibility("hidden")))
void CLASS::CheckSleepCapability(void)
{
bool haveSleep = false;
if (!(gux_options & GUX_OPTION_NO_SLEEP) &&
_device->hasPCIPowerManagement(kPCIPMCPMESupportFromD3Cold) &&
kIOReturnSuccess == _device->enablePCIPowerManagement(kPCIPMCSPowerStateD3))
haveSleep = true;
else
IOLog("%s: xHC will be unloaded across sleep\n", getName());
_expansionData->_controllerCanSleep = haveSleep;
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 || defined(REHABMAN_UNIVERSAL_BUILD)
if (CHECK_FOR_MAVERICKS)
{
WRITE_V3EXPANSION(_hasPCIPwrMgmt, haveSleep);
}
#endif
/*
* Note:
* Always set the Card Type to Built-in, in order
* to enable the extra-current mechanism, even
* if xHC does not support save/restore.
* See IOUSBRootHubDevice::start
*/
#if 0
setProperty("Card Type", haveSleep ? "Built-in" : "PCI");
#else
setProperty("Card Type", "Built-in");
setProperty("ResetOnResume", !haveSleep);
#endif
}
__attribute__((visibility("hidden")))
IOReturn CLASS::RHCompleteSuspendOnAllPorts(void)
{
uint32_t wait, portSC, changePortSC, idbmp, wantedWakeBits;
wait = 0U;
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 || defined(REHABMAN_UNIVERSAL_BUILD)
if (CHECK_FOR_MAVERICKS)
{
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090
idbmp = ((Yosemite_ExpansionData*)_expansionData)->_ignoreDisconnectBitmap;
#else
idbmp = _expansionData->_ignoreDisconnectBitmap;
#endif
}
else
#endif
idbmp = 0U;
for (uint8_t port = 0U; port < _rootHubNumPorts; ++port) {
changePortSC = 0U;
portSC = Read32Reg(&_pXHCIOperationalRegisters->prs[port].PortSC);
if (m_invalid_regspace)
return kIOReturnNoDevice;
if ((_errataBits & kErrataIntelPantherPoint) &&
!(portSC & XHCI_PS_CSC))
changePortSC = XHCI_PS_CSC;
if ((portSC & XHCI_PS_PED) &&
XHCI_PS_PLS_GET(portSC) < XDEV_U3) {
changePortSC |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XDEV_U3);
wait = 15U;
}
if (idbmp & (2U << port))
wantedWakeBits = XHCI_PS_WOE;
else
wantedWakeBits = XHCI_PS_WAKEBITS;
if (wantedWakeBits != (portSC & XHCI_PS_WAKEBITS)) {
portSC &= ~XHCI_PS_WAKEBITS;
changePortSC |= wantedWakeBits;
}
if (changePortSC)
Write32Reg(&_pXHCIOperationalRegisters->prs[port].PortSC, (portSC & XHCI_PS_WRITEBACK_MASK) | changePortSC);
}
if (wait)
IOSleep(wait);
return kIOReturnSuccess;
}
__attribute__((visibility("hidden")))
void CLASS::QuiesceAllEndpoints(void)
{
for (uint8_t slot = 1U; slot <= _numSlots; ++slot) {
SlotStruct* pSlot = SlotPtr(slot);
if (pSlot->isInactive())
continue;
for (int32_t endpoint = 1; endpoint != kUSBMaxPipes; ++endpoint) {
ringStruct* pRing = pSlot->ringArrayForEndpoint[endpoint];
if (!pRing->isInactive())
QuiesceEndpoint(slot, endpoint);
}
}
}
__attribute__((visibility("hidden")))
IOReturn CLASS::ResetController(void)
{
IOReturn rc;
/*
* Wait for Controller Ready in case we're starting from hardware reset
*/
Read32Reg(&_pXHCIOperationalRegisters->USBSts); // Note: CNR may be asserted on 1st read
if (m_invalid_regspace)
return kIOReturnNoDevice;
rc = XHCIHandshake(&_pXHCIOperationalRegisters->USBSts, XHCI_STS_CNR, 0U, 1000);
if (rc != kIOReturnSuccess) {
if (rc == kIOReturnTimeout)
IOLog("%s: chip not ready within 1000 ms\n", __FUNCTION__);
return rc;
}
/*
* Make sure controller is halted in case we're not starting
* from hardware reset
*/
Write32Reg(&_pXHCIOperationalRegisters->USBCmd, 0U);
rc = WaitForUSBSts(XHCI_STS_HCH, UINT32_MAX);
if (rc != kIOReturnSuccess) {
if (rc == kIOReturnTimeout)
IOLog("%s: could not get chip to halt within 100 ms\n", __FUNCTION__);
return rc;
}
if (_errataBits & kErrataFL1100LowRev)
FL1100Tricks(3);
Write32Reg(&_pXHCIOperationalRegisters->USBCmd, XHCI_CMD_HCRST);
rc = XHCIHandshake(&_pXHCIOperationalRegisters->USBCmd, XHCI_CMD_HCRST, 0U, 1000);
if (rc == kIOReturnTimeout)
IOLog("%s: could not get chip to come out of reset within 1000 ms\n", __FUNCTION__);
if (_errataBits & kErrataFL1100LowRev)
FL1100Tricks(4);
return rc;
}
__attribute__((visibility("hidden")))
IOReturn CLASS::StopUSBBus(void)
{
uint32_t cmd = Read32Reg(&_pXHCIOperationalRegisters->USBCmd);
if (m_invalid_regspace)
return kIOReturnNoDevice;
if (!(cmd & XHCI_CMD_RS)) {
_myBusState = kUSBBusStateReset;
return kIOReturnSuccess;
}
Write32Reg(&_pXHCIOperationalRegisters->USBCmd, cmd & ~XHCI_CMD_RS);
IOReturn rc = WaitForUSBSts(XHCI_STS_HCH, UINT32_MAX);
if (rc != kIOReturnTimeout)
_myBusState = kUSBBusStateReset;
return rc;
}
__attribute__((visibility("hidden")))
void CLASS::RestartUSBBus(void)
{
uint32_t cmd = Read32Reg(&_pXHCIOperationalRegisters->USBCmd);
if (m_invalid_regspace)
return;
cmd |= XHCI_CMD_EWE | XHCI_CMD_INTE | XHCI_CMD_RS;
Write32Reg(&_pXHCIOperationalRegisters->USBCmd, cmd);
IOReturn rc = WaitForUSBSts(XHCI_STS_HCH, 0U);
if (rc != kIOReturnNoDevice)
_myBusState = kUSBBusStateRunning;
}
__attribute__((visibility("hidden")))
void CLASS::NotifyRootHubsOfPowerLoss(void)
{
IOUSBHubPolicyMaker *pm2, *pm3;
/*
* TBD: Couldn't find a decent way to notify up-stack
* drivers of controller power-loss and need
* for re-enumeration.
*/
pm2 = GetHubForProtocol(kUSBDeviceSpeedHigh);
pm3 = GetHubForProtocol(kUSBDeviceSpeedSuper);
if (pm2)
#if 1
pm2->changePowerStateTo(kIOUSBHubPowerStateRestart);
#else
pm2->HubPowerChange(kIOUSBHubPowerStateRestart);
#endif
if (pm3)
#if 1
pm3->changePowerStateTo(kIOUSBHubPowerStateRestart);
#else
pm3->HubPowerChange(kIOUSBHubPowerStateRestart);
#endif
}
#if 0
__attribute__((visibility("hidden")))
void CLASS::SantizePortsAfterPowerLoss(void)
{
for (uint8_t port = 0U; port < _rootHubNumPorts; ++port) {
uint32_t portSC = GetPortSCForWriting(port);
if (m_invalid_regspace)
return;
Write32Reg(&_pXHCIOperationalRegisters->prs[port].PortSC, portSC | XHCI_PS_WAKEBITS);
}
}
__attribute__((visibility("hidden")))
void CLASS::DisableWakeBits(void)
{
if (!_wakeEnabled || m_invalid_regspace || isInactive())
return;
for (uint8_t port = 0U; port < _rootHubNumPorts; ++port) {
uint32_t portSC = GetPortSCForWriting(port);
if (m_invalid_regspace)
return;
Write32Reg(&_pXHCIOperationalRegisters->prs[port].PortSC, portSC & ~XHCI_PS_WAKEBITS);
}
_wakeEnabled = false;
}
__attribute__((visibility("hidden")))
void CLASS::EnableWakeBits(void)
{
if (_wakeEnabled || m_invalid_regspace || isInactive())
return;
uint32_t idbmp = _expansionData->_ignoreDisconnectBitmap;
for (uint8_t port = 0U; port < _rootHubNumPorts; ++port) {
uint32_t portSC = GetPortSCForWriting(port);
if (m_invalid_regspace)
return;
if (idbmp & (2U << port))
portSC = (portSC & ~XHCI_PS_WAKEBITS) | XHCI_PS_WOE;
else
portSC |= XHCI_PS_WAKEBITS;
Write32Reg(&_pXHCIOperationalRegisters->prs[port].PortSC, portSC);
}
_wakeEnabled = true;
}
#endif
__attribute__((visibility("hidden")))
void CLASS::SetPropsForBookkeeping(void)
{
/*
* Note:
* IOUSBController::CreateRootHubDevice copies these properties
* from _device to the root hub devices, and also to IOResources.
* The copying to IOResources takes place once (globally).
* IOUSBRootHubDevice::RequestExtraWakePower then uses the global
* values on IOResources to do power-accounting for all root hubs
* in the system (!). So whatever value given here for extra current
* must cover 400mA of extra current for each SS root hub port in
* the system.
* Since there may be more than one xHC, must account for them all.
* Set a high value of 255 to (hopefully) cover everything.
* Additionally, iPhone/iPad ask for 1600mA of extra power on high-speed
* ports, so we allow for that as well.
*/
#if 0
uint32_t defaultTotalExtraCurrent = (kUSB3MaxPowerPerPort - kUSB2MaxPowerPerPort) * _rootHubNumPorts;
uint32_t defaultMaxCurrentPerPort = kUSB3MaxPowerPerPort;
#else
uint32_t defaultTotalExtraCurrent = (kUSB3MaxPowerPerPort - kUSB2MaxPowerPerPort) * 255U;
uint32_t defaultMaxCurrentPerPort = kUSB3MaxPowerPerPort + 1600U;
#endif
/*
* Note: Only set defaults if none were injected via DSDT
*/
if (!OSDynamicCast(OSNumber, _device->getProperty(kAppleMaxPortCurrent)))
_device->setProperty(kAppleMaxPortCurrent, defaultMaxCurrentPerPort, 32U);
if (!OSDynamicCast(OSNumber, _device->getProperty(kAppleCurrentExtra)))
_device->setProperty(kAppleCurrentExtra, defaultTotalExtraCurrent, 32U);
if (!OSDynamicCast(OSNumber, _device->getProperty(kAppleMaxPortCurrentInSleep)))
_device->setProperty(kAppleMaxPortCurrentInSleep, defaultMaxCurrentPerPort, 32U);
if (!OSDynamicCast(OSNumber, _device->getProperty(kAppleCurrentExtraInSleep)))
_device->setProperty(kAppleCurrentExtraInSleep, defaultTotalExtraCurrent, 32U);
}