This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
/
kern_audio.cpp
278 lines (236 loc) · 8.7 KB
/
kern_audio.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
//
// kern_audio.cpp
// NvidiaGraphicsFixup
//
// Copyright © 2017 lvs1974. All rights reserved.
//
#include <Headers/kern_iokit.hpp>
#include <Headers/plugin_start.hpp>
#include "kern_config.hpp"
#include "kern_audio.hpp"
OSDefineMetaClassAndStructors(NVidiaAudio, IOService)
uint32_t NVidiaAudio::getAnalogLayout() {
// For some DP monitors layout-id value should match HDEF layout-id
// If we have HDEF properly configured, get the value
static uint32_t layout = 0;
if (!layout) {
const char *tree[] {"AppleACPIPCI"};
auto sect = WIOKit::findEntryByPrefix("/AppleACPIPlatformExpert", "PCI", gIOServicePlane);
for (size_t i = 0; sect && i < arrsize(tree); i++) {
sect = WIOKit::findEntryByPrefix(sect, tree[i], gIOServicePlane);
if (sect && i+1 == arrsize(tree)) {
auto iterator = sect->getChildIterator(gIOServicePlane);
if (iterator) {
IORegistryEntry *obj = nullptr;
while ((obj = OSDynamicCast(IORegistryEntry, iterator->getNextObject())) != nullptr) {
uint32_t vendor = 0;
// We can technically check the class-code too, but it is not required.
// HDEF and HDAU should have the same layout-id if any, so it is irrelevant which we find.
if (WIOKit::getOSDataValue(obj, "layout-id", layout) &&
WIOKit::getOSDataValue(obj, "vendor-id", vendor) &&
vendor == WIOKit::VendorID::Intel) {
DBGLOG("audio", "found intel audio %s with layout-id %u", safeString(obj->getName()), layout);
return layout;
}
}
iterator->release();
}
}
}
DBGLOG("audio", "failed to find HDEF layout-id, falling back to 1");
layout = 0x1;
}
return layout;
}
IOService *NVidiaAudio::probe(IOService *hdaService, SInt32 *score) {
if (!ADDPR(startSuccess)) {
return nullptr;
}
if (ADDPR(ngfx_config).noaudiofixes) {
DBGLOG("audio", "all audio fixes are disabled");
return nullptr;
}
if (!hdaService) {
DBGLOG("audio", "received null digitial audio device");
return nullptr;
}
uint32_t hdaVen, hdaDev;
if (!WIOKit::getOSDataValue(hdaService, "vendor-id", hdaVen) ||
!WIOKit::getOSDataValue(hdaService, "device-id", hdaDev)) {
SYSLOG("audio", "found an unknown device");
return nullptr;
}
auto hdaPlaneName = safeString(hdaService->getName());
DBGLOG("audio", "corrects digital audio for hdau at %s with %04X:%04X",
hdaPlaneName, hdaVen, hdaDev);
if (hdaVen != VendorID::NVIDIA) {
DBGLOG("audio", "unsupported hdau vendor");
return nullptr;
}
IORegistryEntry *gpuService {nullptr};
// Cannot iterate over siblings, so firstly get to IOPP
auto controller = hdaService->getParentEntry(gIOServicePlane);
if (controller) {
// Then iterate over IOPP children (GFX0 and HDAU)
auto iterator = controller->getChildIterator(gIOServicePlane);
if (iterator) {
while ((gpuService = OSDynamicCast(IORegistryEntry, iterator->getNextObject())) != nullptr) {
uint32_t classCode;
if (WIOKit::getOSDataValue(gpuService, "class-code", classCode)) {
// https://pci-ids.ucw.cz/read/PD/03/00 PCI CLASS VGA COMPATIBLE CONTROLLER
if ((classCode & 0xFFFF00) == 0x030000)
break;
else
DBGLOG("audio", "found incompatible class-code %04X", classCode);
} else {
DBGLOG("audio", "failed to find class-code in %s", safeString(gpuService->getName()));
}
gpuService = nullptr;
}
iterator->release();
} else {
DBGLOG("audio", "hdau parent iterator error");
}
} else {
DBGLOG("audio", "hdau parent error");
}
if (!gpuService) {
DBGLOG("audio", "failed to find gpu service");
return nullptr;
}
uint32_t gpuVen, gpuDev;
if (!WIOKit::getOSDataValue(gpuService, "vendor-id", gpuVen) ||
!WIOKit::getOSDataValue(gpuService, "device-id", gpuDev)) {
SYSLOG("audio", "found an unknown gpu device");
return nullptr;
}
if (gpuService->getProperty("no-audio-autofix")) {
DBGLOG("audio", "asked to avoid messing with digital audio");
return nullptr;
}
auto gpuPlaneName = safeString(gpuService->getName());
DBGLOG("audio", "corrects digital audio for gpu at %s with %04X:%04X", gpuPlaneName, gpuVen, gpuDev);
if (gpuVen != VendorID::NVIDIA) {
DBGLOG("audio", "unsupported GPU vendor");
return nullptr;
}
// Power management may cause issues for non GFXx
if (strncmp(gpuPlaneName, "GFX", strlen("GFX"))) {
DBGLOG("audio", "fixing gpu plane name to GFX0");
gpuService->setName("GFX0");
}
// AppleHDAController only recognises HDEF and HDAU
if (strcmp(hdaPlaneName, "HDAU")) {
DBGLOG("audio", "fixing audio plane name to HDAU");
hdaService->setName("HDAU");
}
// hda-gfx allows to separate the devices, must be unique
auto hdaHdaGfx = hdaService->getProperty("hda-gfx");
auto gpuHdaGfx = gpuService->getProperty("hda-gfx");
if (!hdaHdaGfx && !gpuHdaGfx) {
static uint32_t hdaCounter {0};
static const char *hdaNames[] {
"onboard-2",
"onboard-3",
"onboard-4"
};
if (hdaCounter < arrsize(hdaNames)) {
DBGLOG("audio", "fixing hda-gfx to %s", hdaNames[hdaCounter]);
auto hda = OSData::withBytes(hdaNames[hdaCounter], sizeof("onboard-2"));
hdaService->setProperty("hda-gfx", hda);
gpuService->setProperty("hda-gfx", hda);
hdaCounter++;
} else {
SYSLOG("audio", "out of hda-gfx indexes");
}
} else {
DBGLOG("audio", "existing hda-gfx in gpu (%d) or hdau (%d), assuming complete inject",
gpuHdaGfx != nullptr, hdaHdaGfx != nullptr);
}
// layout-id is heard to be required in rare cases
if (!hdaService->getProperty("layout-id")) {
DBGLOG("audio", "fixing layout-id in hdau");
uint32_t layout = getAnalogLayout();
hdaService->setProperty("layout-id", OSData::withBytes(&layout, sizeof(layout)));
} else {
DBGLOG("audio", "found existing layout-id in hdau");
}
if (!ADDPR(ngfx_config).noaudioconnectors && !gpuService->getProperty("no-audio-fixconn")) {
uint8_t builtBytes[] { 0x00, 0x08, 0x00, 0x00 };
char connector_type[] { "@0,connector-type" };
for (int i=0; i<MaxConnectorCount; ++i)
{
connector_type[1] = '0' + i;
if (!gpuService->getProperty(connector_type)) {
DBGLOG("audio", "fixing %s in gpu", connector_type);
gpuService->setProperty(connector_type, OSData::withBytes(builtBytes, sizeof(builtBytes)));
} else {
DBGLOG("audio", "found existing %s in gpu", connector_type);
}
}
}
// built-in is required for non-renamed devices
if (!hdaService->getProperty("built-in")) {
DBGLOG("audio", "fixing built-in in hdau");
uint8_t builtBytes[] { 0x01, 0x00, 0x00, 0x00 };
hdaService->setProperty("built-in", OSData::withBytes(builtBytes, sizeof(builtBytes)));
} else {
DBGLOG("audio", "found existing built-in in hdau");
}
if (!gpuService->getProperty("built-in")) {
DBGLOG("audio", "fixing built-in in gpu");
uint8_t builtBytes[] { 0x01, 0x00, 0x00, 0x00 };
gpuService->setProperty("built-in", OSData::withBytes(builtBytes, sizeof(builtBytes)));
} else {
DBGLOG("audio", "found existing built-in in gpu");
}
auto gpuSlotName = gpuService->getProperty("AAPL,slot-name");
if (!gpuSlotName) {
static uint32_t slotCounter {0};
static const char *slotNames[] {
"Slot-1",
"Slot-2",
"Slot-3",
"Slot-4"
};
if (slotCounter < arrsize(slotNames)) {
DBGLOG("audio", "fixing AAPL,slot-name to %s", slotNames[slotCounter]);
auto slot = OSData::withBytes(slotNames[slotCounter], sizeof("Slot-2"));
gpuService->setProperty("AAPL,slot-name", slot);
slotCounter++;
} else {
SYSLOG("audio", "out of slot name indexes");
}
} else {
DBGLOG("audio", "existing AAPL,slot-name in gpu (%d), assuming complete inject", gpuSlotName != nullptr);
}
// This may be required for device matching
auto compatibleProp = OSDynamicCast(OSData, hdaService->getProperty("compatible"));
if (compatibleProp) {
uint32_t compatibleSz = compatibleProp->getLength();
auto compatibleStr = static_cast<const char *>(compatibleProp->getBytesNoCopy());
DBGLOG("audio", "compatible property starts with %s and is %u bytes", safeString(compatibleStr), compatibleSz);
if (compatibleStr) {
for (uint32_t i = 0; i < compatibleSz; i++) {
if (!strcmp(&compatibleStr[i], "HDAU")) {
DBGLOG("audio", "found HDAU in compatible, ignoring");
return nullptr;
}
i += strlen(&compatibleStr[i]);
}
uint32_t compatibleBufSz = compatibleSz + sizeof("HDAU");
uint8_t *compatibleBuf = Buffer::create<uint8_t>(compatibleBufSz);
if (compatibleBuf) {
DBGLOG("audio", "fixing compatible to have HDAU");
lilu_os_memcpy(&compatibleBuf[0], compatibleStr, compatibleSz);
lilu_os_memcpy(&compatibleBuf[compatibleSz], "HDAU", sizeof("HDAU"));
hdaService->setProperty("compatible", OSData::withBytes(compatibleBuf, compatibleBufSz));
} else {
SYSLOG("audio", "compatible property memory alloc failure %u", compatibleBufSz);
}
}
} else {
SYSLOG("audio", "compatible property is missing");
}
return nullptr;
}