-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_SetupAPI.cpp
354 lines (320 loc) · 9.4 KB
/
LibXPUInfo_SetupAPI.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
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
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#ifdef XPUINFO_USE_SETUPAPI
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif
#include "LibXPUInfo.h"
#include "LibXPUInfo_Util.h"
#include <initguid.h> // include before devguid.h, devpkey.h in one module
#include <devguid.h>
#include <Devpkey.h>
#include <SetupAPI.h>
#include <sstream>
#include "DebugStream.h"
#pragma comment (lib, "Setupapi.lib")
namespace
{
// Found here, but not sure where they got it: https://wine-devel.winehq.narkive.com/vKO2Bkgj/patch-1-2-dxgi-tests-add-test-for-enumerating-display-adapters-using-setupapi
// Valid on >=Win10
DEFINE_DEVPROPKEY(DEVPROPKEY_DISPLAY_ADAPTER_LUID, 0x60b193cb, 0x5276, 0x4d0f, 0x96, 0xfc, 0xf1, 0x73, 0xab, 0xad, 0x3e, 0xc6, 2); // sizeof(LUID)
/*
Return code Description
ERROR_INVALID_FLAGS
The value of Flags is not zero.
ERROR_INVALID_HANDLE
The device information set that is specified by DevInfoSet is not valid.
ERROR_INVALID_PARAMETER
A supplied parameter is not valid. One possibility is that the device information element is not valid.
ERROR_INVALID_REG_PROPERTY
The property key that is supplied by PropertyKey is not valid.
ERROR_INVALID_DATA
An unspecified internal data value was not valid.
ERROR_INVALID_USER_BUFFER
A user buffer is not valid. One possibility is that PropertyBuffer is NULL and PropertBufferSize is not zero.
ERROR_NO_SUCH_DEVINST
The device instance that is specified by DevInfoData does not exist.
ERROR_INSUFFICIENT_BUFFER
The PropertyBuffer buffer is too small to hold the requested property value, or an internal data buffer that was passed to a system call was too small.
ERROR_NOT_ENOUGH_MEMORY
There was not enough system memory available to complete the operation.
ERROR_NOT_FOUND
The requested device property does not exist.
ERROR_ACCESS_DENIED
The caller does not have Administrator privileges.
*/
#define CASE_ERR_TO_STR(e) case e: str << #e; break;
XI::WString GetErrorCodeStr(DWORD lr)
{
std::wostringstream str;
switch (lr)
{
CASE_ERR_TO_STR(ERROR_INVALID_FLAGS)
CASE_ERR_TO_STR(ERROR_INVALID_HANDLE)
CASE_ERR_TO_STR(ERROR_INVALID_PARAMETER)
CASE_ERR_TO_STR(ERROR_INVALID_REG_PROPERTY)
CASE_ERR_TO_STR(ERROR_INVALID_DATA)
CASE_ERR_TO_STR(ERROR_INVALID_USER_BUFFER)
CASE_ERR_TO_STR(ERROR_NO_SUCH_DEVINST)
CASE_ERR_TO_STR(ERROR_INSUFFICIENT_BUFFER)
CASE_ERR_TO_STR(ERROR_NOT_ENOUGH_MEMORY)
CASE_ERR_TO_STR(ERROR_NOT_FOUND)
CASE_ERR_TO_STR(ERROR_ACCESS_DENIED)
default:
str << L"Unknown Error";
}
return str.str();
}
bool sdiGetProp(HDEVINFO info, std::vector<wchar_t>& tempBuf, PSP_DEVINFO_DATA pDID, const DEVPROPKEY* pDPK, XI::WString& outStr)
{
DEVPROPTYPE PropType;
bool bRet = SetupDiGetDevicePropertyW(
info,
pDID,
pDPK,
&PropType,
(PBYTE)tempBuf.data(),
(DWORD)tempBuf.size(),
nullptr,
0);
if (!bRet)
{
auto lastErr = GetLastError();
XI::DebugStreamW dStr(true);
dStr << L"ERROR: " << __FUNCTIONW__ << L" returned " << GetErrorCodeStr(lastErr) << "(" << lastErr << ")\n";
}
bRet = bRet && (PropType == DEVPROP_TYPE_STRING);
if (bRet)
{
outStr = tempBuf.data();
}
return bRet;
}
}
namespace XI
{
namespace //private
{
bool getInfoForClass(const GUID* devClass, std::vector<DriverInfoPtr>& outInfos)
{
HDEVINFO m_info = SetupDiGetClassDevsW(devClass,
L"PCI", // Filter out remote desktop which is "SWD"
nullptr, DIGCF_PRESENT);
HYBRIDDETECT_DEBUG_REQUIRE(m_info != INVALID_HANDLE_VALUE);
SP_DEVINFO_DATA devInfoData;
ZeroMemory(&devInfoData, sizeof(devInfoData));
devInfoData.cbSize = sizeof(devInfoData);
DWORD devIdx = 0;
while (SetupDiEnumDeviceInfo(m_info, devIdx, &devInfoData))
{
DebugStreamW dStr(false);
DriverInfoPtr curInfo(new DriverInfo);
dStr << L"\tDevice " << devIdx << ": ";
++devIdx;
DWORD numKeys = 0;
if (SetupDiGetDevicePropertyKeys(m_info, &devInfoData, nullptr, 0, &numKeys, 0))
{
dStr << L" with " << numKeys << L" keys";
}
DEVPROPTYPE PropType;
DWORD nameSize = 0;
std::vector<wchar_t> tempStr;
tempStr.resize(256);
/*
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPKEY_Device_EnumeratorName,
&PropType,
(PBYTE)tempStr.data(),
(DWORD)tempStr.size(),
&nameSize,
0))
{
// Should be PCI unless filter above changed
dStr << L" at \"" << tempStr.data() << L"\"";
}
*/
if (sdiGetProp(m_info, tempStr, &devInfoData, &DEVPKEY_Device_DriverDesc, curInfo->DriverDesc))
{
dStr << curInfo->DriverDesc;
}
if (sdiGetProp(m_info, tempStr, &devInfoData, &DEVPKEY_Device_DeviceDesc, curInfo->DeviceDesc))
{
if (curInfo->DeviceDesc != curInfo->DriverDesc)
{
dStr << L", (" << curInfo->DeviceDesc << ")";
}
}
else
{
DWORD err = GetLastError();
dStr << L",(DEVPKEY_Device_DeviceDesc: " << GetErrorCodeStr(err) << ")";
}
if (sdiGetProp(m_info, tempStr, &devInfoData, &DEVPKEY_Device_DriverVersion, curInfo->DriverVersion))
{
dStr << L", " << curInfo->DriverVersion;
}
FILETIME fileTime{};
SYSTEMTIME sysTime{};
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPKEY_Device_DriverDate,
&PropType,
(PBYTE)&fileTime,
sizeof(FILETIME),
nullptr,
0))
{
curInfo->DriverDate = fileTime;
SYSTEMTIME curSysTime;
GetSystemTime(&curSysTime);
if (FileTimeToSystemTime(&fileTime, &sysTime))
{
dStr << L", " << sysTime.wMonth << L"/" << sysTime.wDay << L"/" << sysTime.wYear;
float yearsCur = curSysTime.wYear + curSysTime.wMonth / 12.0f + curSysTime.wDay / 365.25f;
float yearsDriver = sysTime.wYear + sysTime.wMonth / 12.0f + sysTime.wDay / 365.25f;
auto oldPrec = dStr.precision(2);
dStr << L" (" << yearsCur - yearsDriver << " years old)";
dStr.precision(oldPrec);
}
}
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPKEY_Device_InstallDate,
&PropType,
(PBYTE)&fileTime,
sizeof(FILETIME),
nullptr,
0))
{
curInfo->InstallDate = fileTime;
SYSTEMTIME curSysTime;
GetSystemTime(&curSysTime);
if (FileTimeToSystemTime(&fileTime, &sysTime))
{
dStr << L",InstallDate, " << sysTime.wMonth << L"/" << sysTime.wDay << L"/" << sysTime.wYear;
float yearsCur = curSysTime.wYear + curSysTime.wMonth / 12.0f + curSysTime.wDay / 365.25f;
float yearsDriver = sysTime.wYear + sysTime.wMonth / 12.0f + sysTime.wDay / 365.25f;
auto oldPrec = dStr.precision(2);
dStr << L" (" << yearsCur - yearsDriver << " years old)";
dStr.precision(oldPrec);
}
}
if (sdiGetProp(m_info, tempStr, &devInfoData, &DEVPKEY_Device_InstanceId, curInfo->DeviceInstanceId))
{
dStr << L"(" << tempStr.data() << L") ";
}
/*
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPKEY_Device_Service,
&PropType,
(PBYTE)tempStr.data(),
(DWORD)tempStr.size(),
&nameSize,
0))
{
// With filter as PCI, should never be "SWD" or other indicator of software service
dStr << L" service = \"" << tempStr.data() << L"\"";
}
*/
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPKEY_Device_LocationInfo,
&PropType,
(PBYTE)tempStr.data(),
(DWORD)tempStr.size(),
&nameSize,
0))
{
dStr << L" at \"" << tempStr.data() << L"\"";
bool locValid = curInfo->LocationInfo.GetFromWStr(tempStr.data());
if (!locValid)
{
dStr << " ** Error parsing location!\n";
continue;
}
}
// For Intel, this might be an unofficial way to find the "GT Generation"
if (sdiGetProp(m_info, tempStr, &devInfoData, &DEVPKEY_Device_DriverInfSection, curInfo->DriverInfSection))
{
dStr << ", Inf Section = " << curInfo->DriverInfSection;
}
LUID curLUID{};
HYBRIDDETECT_DEBUG_REQUIRE(*(UI64*)&curInfo->DeviceLUID == 0ULL);
if (SetupDiGetDevicePropertyW(
m_info,
&devInfoData,
&DEVPROPKEY_DISPLAY_ADAPTER_LUID,
&PropType,
(PBYTE)&curLUID,
(DWORD)sizeof(LUID),
&nameSize,
0) && (nameSize == sizeof(LUID)))
{
curInfo->DeviceLUID = curLUID;
dStr << ", LUID = " << std::hex << *(UI64*)&curLUID << std::dec;
}
outInfos.emplace_back(std::move(curInfo));
dStr << std::endl;
} // while
if (m_info)
{
SetupDiDestroyDeviceInfoList(m_info);
}
return true;
}
} // private ns
SetupDeviceInfo::SetupDeviceInfo()
{
getInfoForClass(&GUID_DEVCLASS_DISPLAY, m_DevInfoPtrs);
getInfoForClass(&GUID_DEVCLASS_COMPUTEACCELERATOR, m_DevInfoPtrs);
}
const DriverInfoPtr SetupDeviceInfo::getByLUID(const UI64 inLUID) const
{
for (const auto& info : m_DevInfoPtrs)
{
UI64 curLUID = LuidToUI64(info->DeviceLUID);
if (curLUID && (inLUID == curLUID))
{
return info;
}
}
return nullptr;
}
const DriverInfoPtr SetupDeviceInfo::getAtAddress(const PCIAddressType& inAddress) const
{
for (const auto& info : m_DevInfoPtrs)
{
if (inAddress == info->LocationInfo)
{
return info;
}
}
return nullptr;
}
const DriverInfoPtr SetupDeviceInfo::getByName(const WString& inName) const
{
for (const auto& info : m_DevInfoPtrs)
{
if ((inName == info->DriverDesc) || (inName == info->DeviceDesc))
{
return info;
}
}
return nullptr;
}
SetupDeviceInfo::~SetupDeviceInfo()
{
}
} // XI
#else
// Build stub so we don't have to change interface
#include "LibXPUInfo.h"
XI::SetupDeviceInfo::~SetupDeviceInfo() {}
#endif // XPUINFO_USE_SETUPAPI