2 * The PCI Library -- List PCI devices on Win32 via Configuration Manager
4 * Copyright (c) 2021 Pali Rohár <pali@kernel.org>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <ctype.h> /* for isxdigit() */
15 #include <stdio.h> /* for sprintf() */
16 #include <string.h> /* for strlen(), strchr(), strncmp() */
17 #include <wchar.h> /* for wcslen(), wcscpy() */
20 #include "win32-helpers.h"
22 /* Unfortunately MinGW32 toolchain does not provide these cfgmgr32 constants. */
24 #ifndef RegDisposition_OpenExisting
25 #define RegDisposition_OpenExisting 0x00000001
28 #ifndef CM_REGISTRY_SOFTWARE
29 #define CM_REGISTRY_SOFTWARE 0x00000001
32 #ifndef CM_DRP_HARDWAREID
33 #define CM_DRP_HARDWAREID 0x00000002
35 #ifndef CM_DRP_SERVICE
36 #define CM_DRP_SERVICE 0x00000005
38 #ifndef CM_DRP_BUSNUMBER
39 #define CM_DRP_BUSNUMBER 0x00000016
41 #ifndef CM_DRP_ADDRESS
42 #define CM_DRP_ADDRESS 0x0000001D
45 #ifndef CR_INVALID_CONFLICT_LIST
46 #define CR_INVALID_CONFLICT_LIST 0x00000039
48 #ifndef CR_INVALID_INDEX
49 #define CR_INVALID_INDEX 0x0000003A
51 #ifndef CR_INVALID_STRUCTURE_SIZE
52 #define CR_INVALID_STRUCTURE_SIZE 0x0000003B
55 #ifndef fIOD_10_BIT_DECODE
56 #define fIOD_10_BIT_DECODE 0x0004
58 #ifndef fIOD_12_BIT_DECODE
59 #define fIOD_12_BIT_DECODE 0x0008
61 #ifndef fIOD_16_BIT_DECODE
62 #define fIOD_16_BIT_DECODE 0x0010
64 #ifndef fIOD_POSITIVE_DECODE
65 #define fIOD_POSITIVE_DECODE 0x0020
67 #ifndef fIOD_PASSIVE_DECODE
68 #define fIOD_PASSIVE_DECODE 0x0040
70 #ifndef fIOD_WINDOW_DECODE
71 #define fIOD_WINDOW_DECODE 0x0080
74 #define fIOD_PORT_BAR 0x0100
77 #ifndef fMD_WINDOW_DECODE
78 #define fMD_WINDOW_DECODE 0x0040
80 #ifndef fMD_MEMORY_BAR
81 #define fMD_MEMORY_BAR 0x0080
84 #ifndef mMD_Prefetchable
85 #define mMD_Prefetchable fMD_Prefetchable
89 * Unfortunately MinGW32 toolchain does not provide import library for these
90 * cfgmgr32.dll functions. So resolve pointers to these functions at runtime.
91 * MinGW-w64 toolchain provides them also in 32-bit mode.
94 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
96 #ifdef CM_Get_DevNode_Registry_PropertyA
97 #undef CM_Get_DevNode_Registry_PropertyA
99 static CONFIGRET (WINAPI *MyCM_Get_DevNode_Registry_PropertyA)(DEVINST dnDevInst, ULONG ulProperty, PULONG pulRegDataType, PVOID Buffer, PULONG pulLength, ULONG ulFlags);
100 #define CM_Get_DevNode_Registry_PropertyA MyCM_Get_DevNode_Registry_PropertyA
102 #ifdef CM_Get_DevNode_Registry_PropertyW
103 #undef CM_Get_DevNode_Registry_PropertyW
105 static CONFIGRET (WINAPI *MyCM_Get_DevNode_Registry_PropertyW)(DEVINST dnDevInst, ULONG ulProperty, PULONG pulRegDataType, PVOID Buffer, PULONG pulLength, ULONG ulFlags);
106 #define CM_Get_DevNode_Registry_PropertyW MyCM_Get_DevNode_Registry_PropertyW
108 #ifndef CM_Open_DevNode_Key
109 #undef CM_Open_DevNode_Key
111 static CONFIGRET (WINAPI *MyCM_Open_DevNode_Key)(DEVINST dnDevNode, REGSAM samDesired, ULONG ulHardwareProfile, REGDISPOSITION Disposition, PHKEY phkDevice, ULONG ulFlags);
112 #define CM_Open_DevNode_Key MyCM_Open_DevNode_Key
115 resolve_cfgmgr32_functions(void)
119 if (CM_Get_DevNode_Registry_PropertyA && CM_Get_DevNode_Registry_PropertyW && CM_Open_DevNode_Key)
122 cfgmgr32 = GetModuleHandleA("cfgmgr32.dll");
126 CM_Get_DevNode_Registry_PropertyA = (void *)GetProcAddress(cfgmgr32, "CM_Get_DevNode_Registry_PropertyA");
127 CM_Get_DevNode_Registry_PropertyW = (void *)GetProcAddress(cfgmgr32, "CM_Get_DevNode_Registry_PropertyW");
128 CM_Open_DevNode_Key = (void *)GetProcAddress(cfgmgr32, "CM_Open_DevNode_Key");
129 if (!CM_Get_DevNode_Registry_PropertyA || !CM_Get_DevNode_Registry_PropertyW || !CM_Open_DevNode_Key)
138 * cfgmgr32.dll uses custom non-Win32 error numbers which are unsupported by
139 * Win32 APIs like GetLastError() and FormatMessage() functions.
141 * Windows 7 introduced new cfgmgr32.dll function CM_MapCrToWin32Err() for
142 * translating mapping CR_* errors to Win32 errors but most error codes are
143 * not mapped. So this function is unusable.
145 * Error strings for CR_* errors are defined in cmapi.rc file which is
146 * statically linked into some system libraries (e.g. filemgmt.dll,
147 * acledit.dll, netui0.dll or netui2.dll) but due to static linking it is
148 * not possible to access these error strings easily at runtime.
150 * So define own function for translating CR_* errors directly to strings.
153 cr_strerror(CONFIGRET cr_error_id)
155 static char unknown_error[sizeof("Unknown CR error XXXXXXXXXX")];
156 static const char *cr_errors[] = {
157 "The operation completed successfully",
159 "Not enough memory is available to process this command",
160 "A required pointer parameter is invalid",
161 "The ulFlags parameter specified is invalid for this operation",
162 "The device instance handle parameter is not valid",
163 "The supplied resource descriptor parameter is invalid",
164 "The supplied logical configuration parameter is invalid",
165 "CR_INVALID_ARBITRATOR",
166 "CR_INVALID_NODELIST",
167 "CR_DEVNODE_HAS_REQS/CR_DEVINST_HAS_REQS",
168 "The RESOURCEID parameter does not contain a valid RESOURCEID",
169 "CR_DLVXD_NOT_FOUND",
170 "The specified device instance handle does not correspond to a present device",
171 "There are no more logical configurations available",
172 "There are no more resource descriptions available",
173 "This device instance already exists",
174 "The supplied range list parameter is invalid",
176 "A general internal error occurred",
177 "CR_NO_SUCH_LOGICAL_DEV",
178 "The device is disabled for this configuration",
180 "A service or application refused to allow removal of this device",
182 "CR_INVALID_LOAD_TYPE",
183 "An output parameter was too small to hold all the data available",
185 "CR_NO_REGISTRY_HANDLE",
186 "A required entry in the registry is missing or an attempt to write to the registry failed",
187 "The specified Device ID is not a valid Device ID",
188 "One or more parameters were invalid",
190 "CR_DEVLOADER_NOT_READY",
192 "There are no more hardware profiles available",
193 "CR_DEVICE_NOT_THERE",
194 "The specified value does not exist in the registry",
196 "The specified priority is invalid for this operation",
197 "This device cannot be disabled",
203 "The specified key does not exist in the registry",
204 "The specified machine name does not meet the UNC naming conventions",
205 "A general remote communication error occurred",
206 "The machine selected for remote communication is not available at this time",
207 "The Plug and Play service or another required service is not available",
209 "This routine is not implemented in this version of the operating system",
210 "The specified property type is invalid for this operation",
211 "Device interface is active",
212 "No such device interface",
213 "Invalid reference string",
214 "Invalid conflict list",
216 "Invalid structure size"
218 if (cr_error_id <= 0 || cr_error_id >= sizeof(cr_errors)/sizeof(*cr_errors))
220 sprintf(unknown_error, "Unknown CR error %lu", cr_error_id);
221 return unknown_error;
223 return cr_errors[cr_error_id];
227 fmt_validate(const char *s, int len, const char *fmt)
231 for (i = 0; i < len; i++)
232 if (!fmt[i] || (fmt[i] == '#' ? !isxdigit(s[i]) : fmt[i] != s[i]))
239 seq_xdigit_validate(const char *s, int mult, int min)
244 if (len < min*mult || len % mult)
247 for (i = 0; i < len; i++)
255 get_device_service_name(struct pci_access *a, DEVINST devinst, DEVINSTID_A devinst_id, BOOL *supported)
257 ULONG reg_type, reg_size, reg_len;
262 * All data are stored as 7-bit ASCII strings in system but service name is
263 * exception. It can contain UNICODE. Moreover it is passed to other Win32 API
264 * functions and therefore it cannot be converted to 8-bit ANSI string without
265 * data loss. So use wide function CM_Get_DevNode_Registry_PropertyW() in this
266 * case and deal with all wchar_t problems...
270 cr = CM_Get_DevNode_Registry_PropertyW(devinst, CM_DRP_SERVICE, ®_type, NULL, ®_size, 0);
271 if (cr == CR_CALL_NOT_IMPLEMENTED)
276 else if (cr == CR_NO_SUCH_VALUE)
281 else if (cr != CR_SUCCESS &&
282 cr != CR_BUFFER_SMALL)
284 a->warning("Cannot retrieve service name for PCI device %s: %s.", devinst_id, cr_strerror(cr));
288 else if (reg_type != REG_SZ)
290 a->warning("Cannot retrieve service name for PCI device %s: Service name is stored as unknown type 0x%lx.", devinst_id, reg_type);
297 * Returned size is on older Windows versions without nul-term char.
298 * So explicitly increase size and fill nul-term byte.
300 reg_size += sizeof(service_name[0]);
301 service_name = pci_malloc(a, reg_size);
303 cr = CM_Get_DevNode_Registry_PropertyW(devinst, CM_DRP_SERVICE, ®_type, service_name, ®_len, 0);
304 service_name[reg_size/sizeof(service_name[0]) - 1] = 0;
305 if (reg_len > reg_size)
307 pci_mfree(service_name);
311 else if (cr != CR_SUCCESS)
313 a->warning("Cannot retrieve service name for PCI device %s: %s.", devinst_id, cr_strerror(cr));
314 pci_mfree(service_name);
318 else if (reg_type != REG_SZ)
320 a->warning("Cannot retrieve service name for PCI device %s: Service name is stored as unknown type 0x%lx.", devinst_id, reg_type);
321 pci_mfree(service_name);
331 get_driver_path_for_service(struct pci_access *a, LPCWSTR service_name, SC_HANDLE manager)
333 UINT (WINAPI *get_system_root_path)(LPWSTR buffer, UINT size) = NULL;
334 DWORD service_config_size, service_config_len;
335 LPQUERY_SERVICE_CONFIGW service_config = NULL;
336 LPWSTR service_image_path = NULL;
337 SERVICE_STATUS service_status;
338 SC_HANDLE service = NULL;
339 char *driver_path = NULL;
340 int trim_system32 = 0;
348 service = OpenServiceW(manager, service_name, SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS);
351 error = GetLastError();
352 if (error != ERROR_SERVICE_DOES_NOT_EXIST)
353 a->warning("Cannot open service %ls with query rights: %s.", service_name, win32_strerror(error));
357 if (!QueryServiceStatus(service, &service_status))
359 error = GetLastError();
360 a->warning("Cannot query status of service %ls: %s.", service_name, win32_strerror(error));
364 if (service_status.dwCurrentState == SERVICE_STOPPED)
367 if (service_status.dwServiceType != SERVICE_KERNEL_DRIVER)
370 if (!QueryServiceConfigW(service, NULL, 0, &service_config_size))
372 error = GetLastError();
373 if (error != ERROR_INSUFFICIENT_BUFFER)
375 a->warning("Cannot query config of service %ls: %s.", service_name, win32_strerror(error));
380 retry_service_config:
381 service_config = pci_malloc(a, service_config_size);
382 if (!QueryServiceConfigW(service, service_config, service_config_size, &service_config_len))
384 error = GetLastError();
385 if (error == ERROR_INSUFFICIENT_BUFFER)
387 pci_mfree(service_config);
388 service_config_size = service_config_len;
389 goto retry_service_config;
391 a->warning("Cannot query config of service %ls: %s.", service_name, win32_strerror(error));
395 if (service_config->dwServiceType != SERVICE_KERNEL_DRIVER)
399 * Despite QueryServiceConfig() is Win32 API, it returns lpBinaryPathName
400 * (ImagePath registry) in NT format. Unfortunately there is no Win32
401 * function for converting NT paths to Win32 paths. So do it manually and
402 * convert this NT format to human-readable Win32 path format.
406 * GetSystemWindowsDirectoryW() returns path to NT SystemRoot namespace.
407 * Alternativelly path to NT SystemRoot namespace can be constructed by
408 * GetSystemDirectoryW() by trimming "\\system32" from the end of path.
409 * GetSystemWindowsDirectoryW() is not provided in old Windows versions,
410 * so use GetProcAddress() for compatibility with all Windows versions.
412 kernel32 = GetModuleHandleW(L"kernel32.dll");
414 get_system_root_path = (void *)GetProcAddress(kernel32, "GetSystemWindowsDirectoryW");
417 get_system_root_path = &GetSystemDirectoryW;
421 if (!service_config->lpBinaryPathName || !service_config->lpBinaryPathName[0])
423 /* No ImagePath is specified, NT kernel assumes implicit kernel driver path by service name, which is relative to "\\system32\\drivers". */
424 /* GetSystemDirectoryW() returns path to "\\system32" directory on all Windows versions. */
425 system32_len = GetSystemDirectoryW(NULL, 0); /* Returns number of WCHARs plus 1 for nul-term. */
426 service_image_path = pci_malloc(a, sizeof(WCHAR) * (system32_len + sizeof("\\drivers\\")-1 + wcslen(service_name) + sizeof(".sys")-1));
427 system32_len = GetSystemDirectoryW(service_image_path, system32_len); /* Now it returns number of WCHARs without nul-term. */
428 if (system32_len && service_image_path[system32_len-1] != L'\\')
429 service_image_path[system32_len++] = L'\\';
430 wcscpy(service_image_path + system32_len, L"drivers\\");
431 wcscpy(service_image_path + system32_len + sizeof("drivers\\")-1, service_name);
432 wcscpy(service_image_path + system32_len + sizeof("drivers\\")-1 + wcslen(service_name), L".sys");
434 else if (wcsncmp(service_config->lpBinaryPathName, L"\\SystemRoot\\", sizeof("\\SystemRoot\\")-1) == 0)
436 /* ImagePath is in NT SystemRoot namespace, convert to Win32 path via GetSystemWindowsDirectoryW()/GetSystemDirectoryW(). */
437 systemroot_len = get_system_root_path(NULL, 0); /* Returns number of WCHARs plus 1 for nul-term. */
438 service_image_path = pci_malloc(a, sizeof(WCHAR) * (systemroot_len + wcslen(service_config->lpBinaryPathName) - (sizeof("\\SystemRoot")-1)));
439 systemroot_len = get_system_root_path(service_image_path, systemroot_len); /* Now it returns number of WCHARs without nul-term. */
440 if (trim_system32 && systemroot_len && (trim_ptr = wcsrchr(service_image_path, L'\\')) != NULL)
441 systemroot_len = trim_ptr - service_image_path;
442 if (systemroot_len && service_image_path[systemroot_len-1] != L'\\')
443 service_image_path[systemroot_len++] = L'\\';
444 wcscpy(service_image_path + systemroot_len, service_config->lpBinaryPathName + sizeof("\\SystemRoot\\")-1);
446 else if (wcsncmp(service_config->lpBinaryPathName, L"\\??\\UNC\\", sizeof("\\??\\UNC\\")-1) == 0 ||
447 wcsncmp(service_config->lpBinaryPathName, L"\\??\\\\UNC\\", sizeof("\\??\\\\UNC\\")-1) == 0)
449 /* ImagePath is in NT UNC namespace, convert to Win32 UNC path via "\\\\" prefix. */
450 service_image_path = pci_malloc(a, sizeof(WCHAR) * (sizeof("\\\\") + wcslen(service_config->lpBinaryPathName) - (sizeof("\\??\\UNC\\")-1)));
451 /* Namespace separator may be single or double backslash. */
452 driver_path_len = sizeof("\\??\\")-1;
453 if (service_config->lpBinaryPathName[driver_path_len] == L'\\')
455 driver_path_len += sizeof("UNC\\")-1;
456 wcscpy(service_image_path, L"\\\\");
457 wcscpy(service_image_path + sizeof("\\\\")-1, service_config->lpBinaryPathName + driver_path_len);
459 else if (wcsncmp(service_config->lpBinaryPathName, L"\\??\\", sizeof("\\??\\")-1) == 0)
461 /* ImagePath is in NT Global?? namespace, root of the Win32 file namespace, so just remove "\\??\\" prefix to get Win32 path. */
462 service_image_path = pci_malloc(a, sizeof(WCHAR) * (wcslen(service_config->lpBinaryPathName) - (sizeof("\\??\\")-1)));
463 /* Namespace separator may be single or double backslash. */
464 driver_path_len = sizeof("\\??\\")-1;
465 if (service_config->lpBinaryPathName[driver_path_len] == L'\\')
467 wcscpy(service_image_path, service_config->lpBinaryPathName + driver_path_len);
469 else if (service_config->lpBinaryPathName[0] != L'\\')
471 /* ImagePath is relative to the NT SystemRoot namespace, convert to Win32 path via GetSystemWindowsDirectoryW()/GetSystemDirectoryW(). */
472 systemroot_len = get_system_root_path(NULL, 0); /* Returns number of WCHARs plus 1 for nul-term. */
473 service_image_path = pci_malloc(a, sizeof(WCHAR) * (systemroot_len + sizeof("\\")-1 + wcslen(service_config->lpBinaryPathName)));
474 systemroot_len = get_system_root_path(service_image_path, systemroot_len); /* Now it returns number of WCHARs without nul-term. */
475 if (trim_system32 && systemroot_len && (trim_ptr = wcsrchr(service_image_path, L'\\')) != NULL)
476 systemroot_len = trim_ptr - service_image_path;
477 if (systemroot_len && service_image_path[systemroot_len-1] != L'\\')
478 service_image_path[systemroot_len++] = L'\\';
479 wcscpy(service_image_path + systemroot_len, service_config->lpBinaryPathName);
483 /* ImagePath is in some unhandled NT namespace, copy it as is. It cannot be used in Win32 API but libpci user can be still interested in it. */
484 service_image_path = pci_malloc(a, sizeof(WCHAR) * wcslen(service_config->lpBinaryPathName));
485 wcscpy(service_image_path, service_config->lpBinaryPathName);
488 /* Calculate len of buffer needed for conversion from LPWSTR to char*. */
489 driver_path_len = WideCharToMultiByte(CP_ACP, 0, service_image_path, -1, NULL, 0, NULL, NULL);
490 if (driver_path_len <= 0)
492 error = GetLastError();
493 a->warning("Cannot convert kernel driver path from wide string to 8-bit string: %s.", win32_strerror(error));
497 driver_path = pci_malloc(a, driver_path_len);
498 driver_path_len = WideCharToMultiByte(CP_ACP, 0, service_image_path, -1, driver_path, driver_path_len, NULL, NULL);
499 if (driver_path_len <= 0)
501 error = GetLastError();
502 a->warning("Cannot convert kernel driver path from wide string to 8-bit string: %s.", win32_strerror(error));
503 pci_mfree(driver_path);
509 if (service_image_path)
510 pci_mfree(service_image_path);
512 pci_mfree(service_config);
514 CloseServiceHandle(service);
519 get_device_driver_devreg(struct pci_access *a, DEVINST devinst, DEVINSTID_A devinst_id)
524 cr = CM_Open_DevNode_Key(devinst, KEY_READ, 0, RegDisposition_OpenExisting, &key, CM_REGISTRY_SOFTWARE);
525 if (cr != CR_SUCCESS)
527 if (cr != CR_NO_SUCH_VALUE)
528 a->warning("Cannot retrieve driver key for device %s: %s.", devinst_id, cr_strerror(cr));
536 read_reg_key_string_value(struct pci_access *a, HKEY key, const char *name, DWORD *unkn_reg_type)
538 DWORD reg_type, reg_size, reg_len;
543 error = RegQueryValueExA(key, name, NULL, ®_type, NULL, ®_size);
544 if (error != ERROR_SUCCESS &&
545 error != ERROR_MORE_DATA)
550 else if (reg_type != REG_SZ)
553 *unkn_reg_type = reg_type;
558 value = pci_malloc(a, reg_size + 1);
560 error = RegQueryValueExA(key, name, NULL, ®_type, (PBYTE)value, ®_len);
561 if (error != ERROR_SUCCESS)
564 if (error == ERROR_MORE_DATA)
572 else if (reg_type != REG_SZ)
576 *unkn_reg_type = reg_type;
579 value[reg_len] = '\0';
585 driver_cmp(const char *driver, const char *match)
587 int len = strlen(driver);
588 if (driver[0] == '*')
590 if (len >= 4 && strcasecmp(driver + len - 4, ".vxd") == 0)
592 return strncasecmp(driver, match, len);
596 get_driver_path_for_regkey(struct pci_access *a, DEVINSTID_A devinst_id, HKEY key)
598 char *driver_list, *driver, *driver_next;
599 char *subdriver, *subname;
610 driver_list = read_reg_key_string_value(a, key, "DevLoader", &unkn_reg_type);
613 error = GetLastError();
615 a->warning("Cannot read driver DevLoader key for PCI device %s: DevLoader key is stored as unknown type 0x%lx.", devinst_id, unkn_reg_type);
616 else if (error != ERROR_FILE_NOT_FOUND)
617 a->warning("Cannot read driver DevLoader key for PCI device %s: %s.", devinst_id, win32_strerror(error));
622 driver = driver_list;
625 driver_next = strchr(driver, ',');
627 *(driver_next++) = '\0';
629 if (driver_cmp(driver, "ios") == 0 ||
630 driver_cmp(driver, "vcomm") == 0)
631 subname = "PortDriver";
632 else if (driver_cmp(driver, "ntkern") == 0)
633 subname = "NTMPDriver";
634 else if (driver_cmp(driver, "ndis") == 0)
635 subname = "DeviceVxDs";
636 else if (driver_cmp(driver, "vdd") == 0)
642 if (subname && strcmp(subname, "minivdd") == 0)
644 error = RegOpenKeyA(key, "Default", &subkey);
645 if (error != ERROR_SUCCESS)
647 a->warning("Cannot open driver subkey Default for PCI device %s: %s.", devinst_id, win32_strerror(error));
658 subdriver = read_reg_key_string_value(a, subkey, subname, &unkn_reg_type);
661 error = GetLastError();
663 a->warning("Cannot read driver %s key for PCI device %s: %s key is stored as unknown type 0x%lx.", subname, devinst_id, subname, unkn_reg_type);
664 else if (error != ERROR_FILE_NOT_FOUND)
665 a->warning("Cannot read driver %s key for PCI device %s: %s.", subname, devinst_id, win32_strerror(error));
666 else if (strcmp(subname, "minivdd") == 0)
671 else if (strcmp(subname, "drv") == 0)
683 char *endptr = strchr(subdriver, ',');
690 driver = driver_next;
693 if (subdriver && subdriver[0])
694 driver_ptr = subdriver;
700 if (driver_ptr && driver_ptr[0] == '*')
711 len = driver_ptr ? strlen(driver_ptr) : 0;
712 noext = driver_ptr && (len < 4 || driver_ptr[len-4] != '.');
718 if (tolower(driver_ptr[0]) >= 'a' && tolower(driver_ptr[0]) <= 'z' && driver_ptr[1] == ':')
720 /* Driver is already with absolute path. */
721 driver_path = pci_strdup(a, driver_ptr);
723 else if (driver_cmp(driver, "ntkern") == 0 && subdriver)
725 /* Driver is relative to system32\drivers\ directory which is relative to windows directory. */
726 systemdir_len = GetWindowsDirectoryA(NULL, 0);
727 driver_path = pci_malloc(a, systemdir_len + 1 + sizeof("system32\\drivers\\")-1 + strlen(driver_ptr) + 4 + 1);
728 systemdir_len = GetWindowsDirectoryA(driver_path, systemdir_len + 1);
729 if (systemdir_len && driver_path[systemdir_len - 1] != '\\')
730 driver_path[systemdir_len++] = '\\';
731 sprintf(driver_path + systemdir_len, "system32\\drivers\\%s%s", driver_ptr, noext ? ".sys" : "");
735 /* Driver is packed in vmm32.vxd which is stored in system directory. */
736 systemdir_len = GetSystemDirectoryA(NULL, 0);
737 driver_path = pci_malloc(a, systemdir_len + 1 + sizeof("vmm32.vxd ()")-1 + strlen(driver_ptr) + 4 + 1);
738 systemdir_len = GetSystemDirectoryA(driver_path, systemdir_len + 1);
739 if (systemdir_len && driver_path[systemdir_len - 1] != '\\')
740 driver_path[systemdir_len++] = '\\';
741 sprintf(driver_path + systemdir_len, "vmm32.vxd (%s%s)", driver_ptr, noext ? ".vxd" : "");
745 /* Otherwise driver is relative to system directory. */
746 systemdir_len = GetSystemDirectoryA(NULL, 0);
747 driver_path = pci_malloc(a, systemdir_len + 1 + strlen(driver_ptr) + 4 + 1);
748 systemdir_len = GetSystemDirectoryA(driver_path, systemdir_len + 1);
749 if (systemdir_len && driver_path[systemdir_len - 1] != '\\')
750 driver_path[systemdir_len++] = '\\';
751 sprintf(driver_path + systemdir_len, "%s%s", driver_ptr, noext ? ".vxd" : "");
756 pci_mfree(subdriver);
757 pci_mfree(driver_list);
762 get_device_driver_path(struct pci_dev *d, SC_HANDLE manager, BOOL manager_supported)
764 struct pci_access *a = d->access;
765 BOOL service_supported = TRUE;
766 DEVINSTID_A devinst_id = NULL;
767 LPWSTR service_name = NULL;
768 ULONG devinst_id_len = 0;
769 char *driver_path = NULL;
770 DEVINST devinst = (DEVINST)d->backend_data;
775 if (CM_Get_DevNode_Status(&status, &problem, devinst, 0) != CR_SUCCESS || !(status & DN_DRIVER_LOADED))
778 if (CM_Get_Device_ID_Size(&devinst_id_len, devinst, 0) == CR_SUCCESS)
780 devinst_id = pci_malloc(a, devinst_id_len + 1);
781 if (CM_Get_Device_IDA(devinst, devinst_id, devinst_id_len + 1, 0) != CR_SUCCESS)
783 pci_mfree(devinst_id);
784 devinst_id = pci_strdup(a, "UNKNOWN");
788 devinst_id = pci_strdup(a, "UNKNOWN");
790 service_name = get_device_service_name(d->access, devinst, devinst_id, &service_supported);
791 if ((!service_name || !manager) && service_supported && manager_supported)
793 else if (service_name && manager)
795 driver_path = get_driver_path_for_service(d->access, service_name, manager);
799 key = get_device_driver_devreg(d->access, devinst, devinst_id);
802 driver_path = get_driver_path_for_regkey(d->access, devinst_id, key);
810 pci_mfree(service_name);
811 pci_mfree(devinst_id);
816 fill_drivers(struct pci_access *a)
818 BOOL manager_supported;
824 /* ERROR_CALL_NOT_IMPLEMENTED is returned on systems without Service Manager support. */
825 manager_supported = TRUE;
826 manager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
829 error = GetLastError();
830 if (error != ERROR_CALL_NOT_IMPLEMENTED)
831 a->warning("Cannot open Service Manager with connect right: %s.", win32_strerror(error));
833 manager_supported = FALSE;
836 for (d = a->devices; d; d = d->next)
838 driver = get_device_driver_path(d, manager, manager_supported);
841 pci_set_property(d, PCI_FILL_DRIVER, driver);
844 d->known_fields |= PCI_FILL_DRIVER;
848 CloseServiceHandle(manager);
852 res_id_to_str(RESOURCEID res_id)
854 static char hex_res_id[sizeof("0xffffffff")];
856 if (res_id == ResType_IO)
858 else if (res_id == ResType_Mem)
860 else if (res_id == ResType_IRQ)
863 sprintf(hex_res_id, "0x%lx", res_id);
868 fill_resources(struct pci_dev *d, DEVINST devinst, DEVINSTID_A devinst_id)
870 struct pci_access *a = d->access;
878 RES_DES prev_res_des;
887 int last_shared_irq = -1;
889 cr = CM_Get_DevNode_Status(&status, &problem, devinst, 0);
890 if (cr != CR_SUCCESS)
892 a->warning("Cannot retrieve status of PCI device %s: %s.", devinst_id, cr_strerror(cr));
896 cr = CR_NO_MORE_LOG_CONF;
899 * If the device is running then retrieve allocated configuration by PnP
900 * manager which is currently in use by a device.
902 if (!(status & DN_HAS_PROBLEM))
903 cr = CM_Get_First_Log_Conf(&config, devinst, ALLOC_LOG_CONF);
906 * If the device is not running or it does not have allocated configuration by
907 * PnP manager then retrieve forced configuration which prevents PnP manager
908 * from assigning resources.
910 if (cr == CR_NO_MORE_LOG_CONF)
911 cr = CM_Get_First_Log_Conf(&config, devinst, FORCED_LOG_CONF);
914 * If the device does not have neither allocated configuration by PnP manager
915 * nor forced configuration and it is not disabled in the BIOS then retrieve
916 * boot configuration supplied by the BIOS.
918 if (cr == CR_NO_MORE_LOG_CONF &&
919 (!(status & DN_HAS_PROBLEM) || problem != CM_PROB_HARDWARE_DISABLED))
920 cr = CM_Get_First_Log_Conf(&config, devinst, BOOT_LOG_CONF);
922 if (cr != CR_SUCCESS)
925 * Note: Starting with Windows 8, CM_Get_First_Log_Conf returns
926 * CR_CALL_NOT_IMPLEMENTED when used in a Wow64 scenario.
927 * To request information about the hardware resources on a local machine
928 * it is necessary implement an architecture-native version of the
929 * application using the hardware resource APIs. For example: An AMD64
930 * application for AMD64 systems.
932 if (cr == CR_CALL_NOT_IMPLEMENTED && win32_is_32bit_on_win8_64bit_system())
934 static BOOL warn_once = FALSE;
938 a->warning("Cannot retrieve resources of PCI devices from 32-bit application on 64-bit system.");
941 else if (cr != CR_NO_MORE_LOG_CONF)
942 a->warning("Cannot retrieve resources of PCI device %s: %s.", devinst_id, cr_strerror(cr));
947 non_nt_system = win32_is_non_nt_system();
954 ULONG child_name_len;
958 if (CM_Get_Child(&child, devinst, 0) != CR_SUCCESS)
960 else if (CM_Get_Device_ID_Size(&child_name_len, child, 0) != CR_SUCCESS)
965 child_name = pci_malloc(a, child_name_len);
966 if (CM_Get_Device_IDA(child, child_name, child_name_len, 0) != CR_SUCCESS)
968 else if (strncmp(child_name, "PCI\\", 4) != 0)
972 pci_mfree(child_name);
975 if (has_child || d->device_class == PCI_CLASS_BRIDGE_PCI || d->device_class == PCI_CLASS_BRIDGE_CARDBUS)
984 prev_res_des = (RES_DES)config;
985 while ((cr = CM_Get_Next_Res_Des(&res_des, prev_res_des, ResType_All, &res_id, 0)) == CR_SUCCESS)
987 pciaddr_t start, end, size, flags;
988 ULONG res_des_data_size;
991 if (prev_res_des != config)
992 CM_Free_Res_Des_Handle(prev_res_des);
994 prev_res_des = res_des;
996 /* Skip other resources early */
997 if (res_id != ResType_IO && res_id != ResType_Mem && res_id != ResType_IRQ)
1000 cr = CM_Get_Res_Des_Data_Size(&res_des_data_size, res_des, 0);
1001 if (cr != CR_SUCCESS)
1003 a->warning("Cannot retrieve %s resource data of PCI device %s: %s.", res_id_to_str(res_id), devinst_id, cr_strerror(cr));
1007 if (!res_des_data_size)
1009 a->warning("Cannot retrieve %s resource data of PCI device %s: %s.", res_id_to_str(res_id), devinst_id, "Empty data");
1013 res_des_data = pci_malloc(a, res_des_data_size);
1014 cr = CM_Get_Res_Des_Data(res_des, res_des_data, res_des_data_size, 0);
1015 if (cr != CR_SUCCESS)
1017 a->warning("Cannot retrieve %s resource data of PCI device %s: %s.", res_id_to_str(res_id), devinst_id, cr_strerror(cr));
1018 pci_mfree(res_des_data);
1023 * There can be more resources with the same id. In this case we are
1024 * interested in the last one in the list as at the beginning of the list
1025 * can be some virtual resources (which are not set in PCI config space).
1028 if (res_id == ResType_IO)
1030 PIO_RESOURCE io_data = (PIO_RESOURCE)res_des_data;
1032 start = io_data->IO_Header.IOD_Alloc_Base;
1033 end = io_data->IO_Header.IOD_Alloc_End;
1034 size = (end > start) ? (end - start + 1) : 0;
1035 flags = PCI_IORESOURCE_IO;
1038 * If neither 10-bit, 12-bit, nor 16-bit support is presented then
1039 * expects that this is 32-bit I/O resource. If resource does not fit
1040 * into 16-bit space then it must be 32-bit. If PCI I/O resource is
1041 * not 32-bit then it is 16-bit.
1043 if (end <= 0xffff && (io_data->IO_Header.IOD_DesFlags & (fIOD_10_BIT_DECODE|fIOD_12_BIT_DECODE|fIOD_16_BIT_DECODE)))
1044 flags |= PCI_IORESOURCE_IO_16BIT_ADDR;
1047 * 16/32-bit non-NT systems do not support these two flags.
1048 * Most NT-based Windows versions support only the fIOD_WINDOW_DECODE
1049 * flag and put all BAR resources before window resources in this
1050 * resource list. So use this fIOD_WINDOW_DECODE flag as separator
1051 * between IO/MEM windows and IO/MEM BARs of PCI Bridges.
1053 if (io_data->IO_Header.IOD_DesFlags & fIOD_WINDOW_DECODE)
1055 else if (io_data->IO_Header.IOD_DesFlags & fIOD_PORT_BAR)
1058 if (is_bar_res && bar_res_count < 6)
1060 d->flags[bar_res_count] = flags;
1061 d->base_addr[bar_res_count] = start;
1062 d->size[bar_res_count] = size;
1065 else if (!is_bar_res)
1067 d->bridge_flags[0] = flags;
1068 d->bridge_base_addr[0] = start;
1069 d->bridge_size[0] = size;
1070 d->known_fields |= PCI_FILL_BRIDGE_BASES;
1073 else if (res_id == ResType_Mem)
1075 PMEM_RESOURCE mem_data = (PMEM_RESOURCE)res_des_data;
1077 start = mem_data->MEM_Header.MD_Alloc_Base;
1078 end = mem_data->MEM_Header.MD_Alloc_End;
1079 size = (end > start) ? (end - start + 1) : 0;
1080 flags = PCI_IORESOURCE_MEM;
1083 * If fMD_PrefetchAllowed flag is set then this is
1084 * PCI Prefetchable Memory resource.
1086 if ((mem_data->MEM_Header.MD_Flags & mMD_Prefetchable) == fMD_PrefetchAllowed)
1087 flags |= PCI_IORESOURCE_PREFETCH;
1089 /* If resource does not fit into 32-bit space then it must be 64-bit. */
1090 if (is_bar_res && end > 0xffffffff)
1091 flags |= PCI_IORESOURCE_MEM_64;
1094 * These two flags (fMD_WINDOW_DECODE and fMD_MEMORY_BAR) are
1095 * unsupported on most Windows versions, so distinguish between
1096 * window and BAR based on previous resource type.
1098 if (mem_data->MEM_Header.MD_Flags & fMD_WINDOW_DECODE)
1100 else if (mem_data->MEM_Header.MD_Flags & fMD_MEMORY_BAR)
1103 /* 64-bit BAR resource must be at even position. */
1104 if (is_bar_res && (flags & PCI_IORESOURCE_MEM_64) && bar_res_count % 2)
1107 if (is_bar_res && bar_res_count < 6)
1109 d->flags[bar_res_count] = flags;
1110 d->base_addr[bar_res_count] = start;
1111 d->size[bar_res_count] = size;
1113 /* 64-bit BAR resource occupies two slots. */
1114 if (flags & PCI_IORESOURCE_MEM_64)
1117 else if (!is_bar_res && !(flags & PCI_IORESOURCE_PREFETCH))
1119 d->bridge_flags[1] = flags;
1120 d->bridge_base_addr[1] = start;
1121 d->bridge_size[1] = size;
1122 d->known_fields |= PCI_FILL_BRIDGE_BASES;
1124 else if (!is_bar_res && (flags & PCI_IORESOURCE_PREFETCH))
1126 d->bridge_flags[2] = flags;
1127 d->bridge_base_addr[2] = start;
1128 d->bridge_size[2] = size;
1129 d->known_fields |= PCI_FILL_BRIDGE_BASES;
1132 else if (res_id == ResType_IRQ)
1134 PIRQ_RESOURCE irq_data = (PIRQ_RESOURCE)res_des_data;
1137 * libpci's d->irq should be set to the non-MSI PCI IRQ and therefore
1138 * it should be level IRQ which may be shared with other PCI devices
1139 * and drivers in the system. As always we want to retrieve the last
1140 * IRQ number from the resource list.
1142 * On 16/32-bit non-NT systems is fIRQD_Level set to 2 but on NT
1143 * systems to 0. Moreover it looks like that different PCI drivers
1144 * on both NT and non-NT systems set bits 0 and 1 to wrong values
1145 * and so reported value in this list may be incorrect.
1147 * Therefore take the last level-shared IRQ number from the resource
1148 * list and if there is none of this type then take the last IRQ
1149 * number from the list.
1151 last_irq = irq_data->IRQ_Header.IRQD_Alloc_Num;
1152 if ((irq_data->IRQ_Header.IRQD_Flags & (mIRQD_Share|mIRQD_Edge_Level)) == (fIRQD_Share|fIRQD_Level))
1153 last_shared_irq = irq_data->IRQ_Header.IRQD_Alloc_Num;
1156 * IRQ resource on 16/32-bit non-NT systems is separator between
1157 * IO/MEM windows and IO/MEM BARs of PCI Bridges. After the IRQ
1158 * resource are IO/MEM BAR resources.
1160 if (!is_bar_res && non_nt_system)
1164 pci_mfree(res_des_data);
1166 if (cr != CR_NO_MORE_RES_DES)
1167 a->warning("Cannot retrieve resources of PCI device %s: %s.", devinst_id, cr_strerror(cr));
1169 if (prev_res_des != config)
1170 CM_Free_Res_Des_Handle(prev_res_des);
1172 CM_Free_Log_Conf_Handle(config);
1174 /* Set the last IRQ from the resource list to pci_dev. */
1175 if (last_shared_irq >= 0)
1176 d->irq = last_shared_irq;
1177 else if (last_irq >= 0)
1179 if (last_shared_irq >= 0 || last_irq >= 0)
1180 d->known_fields |= PCI_FILL_IRQ;
1182 if (bar_res_count > 0)
1183 d->known_fields |= PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS;
1187 get_device_location(struct pci_access *a, DEVINST devinst, DEVINSTID_A devinst_id, unsigned int *domain, unsigned int *bus, unsigned int *dev, unsigned int *func)
1189 ULONG reg_type, reg_len;
1191 BOOL have_bus, have_devfunc;
1192 DWORD drp_bus_num, drp_address;
1196 have_devfunc = FALSE;
1199 * DRP_BUSNUMBER consists of PCI domain number in high 24 bits
1200 * and PCI bus number in low 8 bits.
1202 reg_len = sizeof(drp_bus_num);
1203 cr = CM_Get_DevNode_Registry_PropertyA(devinst, CM_DRP_BUSNUMBER, ®_type, &drp_bus_num, ®_len, 0);
1204 if (cr == CR_SUCCESS && reg_type == REG_DWORD && reg_len == sizeof(drp_bus_num))
1206 *domain = drp_bus_num >> 8;
1207 *bus = drp_bus_num & 0xff;
1212 * DRP_ADDRESS consists of PCI device number in high 16 bits
1213 * and PCI function number in low 16 bits.
1215 reg_len = sizeof(drp_address);
1216 cr = CM_Get_DevNode_Registry_PropertyA(devinst, CM_DRP_ADDRESS, ®_type, &drp_address, ®_len, 0);
1217 if (cr == CR_SUCCESS && reg_type == REG_DWORD && reg_len == sizeof(drp_address))
1219 *dev = drp_address >> 16;
1220 *func = drp_address & 0xffff;
1221 have_devfunc = TRUE;
1225 * Device Instance Id for PCI devices is of format:
1226 * "<enumerator>\\<device_id>\\<instance_id>"
1228 * "<enumerator>" is "PCI"
1229 * "<device_id>" is "VEN_####&DEV_####&SUBSYS_########&REV_##"
1230 * and "<instance_id>" for PCI devices is at least in one of following format:
1231 * "BUS_##&DEV_##&FUNC_##"
1232 * "##.." (sequence of devfn hex bytes, where bytes represents tree path to the root)
1233 * "#..&#..&#..&#.." (four hex numbers separated by "&"; meaning is unknown yet)
1235 * First two formats are used only on systems without support for multiple
1236 * domains. The second format uses intel-conf encoding of device and function
1237 * number: Low 3 bits is function number and high 5 bits is device number.
1238 * Bus number is not really encoded in second format!
1240 * The third format is used on systems with support for multiple domains but
1241 * format is variable length and currently its meaning is unknown. Apparently
1242 * it looks like that DRP_BUSNUMBER and DRP_ADDRESS registry properties are
1243 * supported on these systems.
1245 * If DRP_BUSNUMBER or DRP_ADDRESS failed then try to parse PCI bus, device
1246 * and function numbers from Instance Id part.
1248 if (!have_bus || !have_devfunc)
1250 const char *device_id0 = strchr(devinst_id, '\\');
1251 const char *instance_id0 = device_id0 ? strchr(device_id0 + 1, '\\') : NULL;
1252 const char *instance_id = instance_id0 ? instance_id0 + 1 : NULL;
1257 if (fmt_validate(instance_id, strlen(instance_id), "BUS_##&DEV_##&FUNC_##") &&
1258 sscanf(instance_id, "BUS_%x&DEV_%x&FUNC_%x", bus, dev, func) == 3)
1261 have_devfunc = TRUE;
1263 else if (seq_xdigit_validate(instance_id, 2, 2) &&
1264 sscanf(instance_id, "%2x", &devfn) == 1)
1267 *func = devfn & 0x7;
1268 have_devfunc = TRUE;
1274 * Virtual IRQ holder devices do not have assigned any bus/dev/func number and
1275 * have "IRQHOLDER" in their Device Id part. So skip them.
1277 if (!have_bus && !have_devfunc && strncmp(devinst_id, "PCI\\IRQHOLDER\\", 14) == 0)
1281 * When some numbers cannot be retrieved via cfgmgr32 then set them to zeros
1282 * to have structure initialized. It makes sense to report via libpci also
1283 * such "incomplete" device as cfgmgr32 can provide additional information
1284 * like device/vendor ids or assigned resources.
1286 if (!have_bus && !have_devfunc)
1288 *bus = *dev = *func = 0;
1289 a->warning("Cannot retrieve bus, device and function numbers for PCI device %s: %s.", devinst_id, cr_strerror(cr));
1294 a->warning("Cannot retrieve bus number for PCI device %s: %s.", devinst_id, cr_strerror(cr));
1296 else if (!have_devfunc)
1299 a->warning("Cannot retrieve device and function numbers for PCI device %s: %s.", devinst_id, cr_strerror(cr));
1306 fill_data_from_string(struct pci_dev *d, const char *str)
1308 BOOL have_device_id;
1309 BOOL have_vendor_id;
1312 const char *endptr, *endptr2;
1316 have_device_id = have_vendor_id = (d->known_fields & PCI_FILL_IDENT);
1317 have_prog_if = have_rev_id = (d->known_fields & PCI_FILL_CLASS_EXT);
1321 endptr = strchr(str, '&');
1322 endptr2 = strchr(str, '\\');
1323 if (endptr2 && (!endptr || endptr > endptr2))
1325 len = endptr ? endptr-str : (int)strlen(str);
1327 if (!have_vendor_id &&
1328 fmt_validate(str, len, "VEN_####") &&
1329 sscanf(str, "VEN_%x", &hex) == 1)
1332 have_vendor_id = TRUE;
1334 else if (!have_device_id &&
1335 fmt_validate(str, len, "DEV_####") &&
1336 sscanf(str, "DEV_%x", &hex) == 1)
1339 have_device_id = TRUE;
1341 else if (!(d->known_fields & PCI_FILL_SUBSYS) &&
1342 fmt_validate(str, len, "SUBSYS_########") &&
1343 sscanf(str, "SUBSYS_%x", &hex) == 1)
1345 d->subsys_vendor_id = hex & 0xffff;
1346 d->subsys_id = hex >> 16;
1347 d->known_fields |= PCI_FILL_SUBSYS;
1349 else if (!have_rev_id &&
1350 fmt_validate(str, len, "REV_##") &&
1351 sscanf(str, "REV_%x", &hex) == 1)
1356 else if (!((d->known_fields & PCI_FILL_CLASS) && have_prog_if) &&
1357 (fmt_validate(str, len, "CC_####") || fmt_validate(str, len, "CC_######")) &&
1358 sscanf(str, "CC_%x", &hex) == 1)
1364 d->prog_if = hex & 0xff;
1365 have_prog_if = TRUE;
1369 if (!(d->known_fields & PCI_FILL_CLASS))
1371 d->device_class = hex;
1372 d->known_fields |= PCI_FILL_CLASS;
1376 if (!endptr || endptr == endptr2)
1382 if ((have_device_id || d->device_id) && (have_vendor_id || d->vendor_id))
1383 d->known_fields |= PCI_FILL_IDENT;
1385 if ((have_prog_if || d->prog_if) && (have_rev_id || d->rev_id))
1386 d->known_fields |= PCI_FILL_CLASS_EXT;
1390 fill_data_from_devinst_id(struct pci_dev *d, DEVINSTID_A devinst_id)
1392 const char *device_id;
1394 device_id = strchr(devinst_id, '\\');
1400 * Device Id part of Device Instance Id is in format:
1401 * "VEN_####&DEV_####&SUBSYS_########&REV_##"
1403 fill_data_from_string(d, device_id);
1407 fill_data_from_hardware_ids(struct pci_dev *d, DEVINST devinst, DEVINSTID_A devinst_id)
1409 ULONG reg_type, reg_size, reg_len;
1410 struct pci_access *a = d->access;
1411 char *hardware_ids = NULL;
1416 cr = CM_Get_DevNode_Registry_PropertyA(devinst, CM_DRP_HARDWAREID, ®_type, NULL, ®_size, 0);
1417 if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL)
1419 a->warning("Cannot retrieve hardware ids for PCI device %s: %s.", devinst_id, cr_strerror(cr));
1422 else if (reg_type != REG_MULTI_SZ && reg_type != REG_SZ) /* Older Windows versions return REG_SZ and new versions REG_MULTI_SZ. */
1424 a->warning("Cannot retrieve hardware ids for PCI device %s: Hardware ids are stored as unknown type 0x%lx.", devinst_id, reg_type);
1430 * Returned size is on older Windows versions without nul-term char.
1431 * So explicitly increase size and fill nul-term byte.
1434 hardware_ids = pci_malloc(a, reg_size);
1436 cr = CM_Get_DevNode_Registry_PropertyA(devinst, CM_DRP_HARDWAREID, ®_type, hardware_ids, ®_len, 0);
1437 hardware_ids[reg_size - 1] = 0;
1438 if (reg_len > reg_size)
1440 pci_mfree(hardware_ids);
1444 else if (cr != CR_SUCCESS)
1446 a->warning("Cannot retrieve hardware ids for PCI device %s: %s.", devinst_id, cr_strerror(cr));
1447 pci_mfree(hardware_ids);
1450 else if (reg_type != REG_MULTI_SZ && reg_type != REG_SZ) /* Older Windows versions return REG_SZ and new versions REG_MULTI_SZ. */
1452 a->warning("Cannot retrieve hardware ids for PCI device %s: Hardware ids are stored as unknown type 0x%lx.", devinst_id, reg_type);
1453 pci_mfree(hardware_ids);
1458 * Hardware ids is nul-separated nul-term string list where each string has
1459 * one of the following format:
1460 * "PCI\\VEN_####&DEV_####&SUBSYS_########&REV_##"
1461 * "PCI\\VEN_####&DEV_####&SUBSYS_########"
1462 * "PCI\\VEN_####&DEV_####&REV_##&CC_####"
1463 * "PCI\\VEN_####&DEV_####&CC_######"
1464 * "PCI\\VEN_####&DEV_####&CC_####"
1465 * "PCI\\VEN_####&DEV_####&REV_##"
1466 * "PCI\\VEN_####&DEV_####"
1468 for (str = hardware_ids; *str != '\0'; str += strlen(str) + 1)
1470 if (strncmp(str, "PCI\\", 4) != 0)
1473 fill_data_from_string(d, str);
1476 pci_mfree(hardware_ids);
1480 scan_devinst_id(struct pci_access *a, DEVINSTID_A devinst_id)
1482 unsigned int domain, bus, dev, func;
1487 cr = CM_Locate_DevNodeA(&devinst, devinst_id, CM_LOCATE_DEVNODE_NORMAL);
1488 if (cr != CR_SUCCESS)
1490 /* Do not show warning when device is not present (= does not match NORMAL flag). */
1491 if (cr != CR_NO_SUCH_DEVNODE)
1492 a->warning("Cannot retrieve handle for device %s: %s.", devinst_id, cr_strerror(cr));
1496 /* get_device_location() returns FALSE if devinst is not real PCI device. */
1497 if (!get_device_location(a, devinst, devinst_id, &domain, &bus, &dev, &func))
1500 d = pci_get_dev(a, domain, bus, dev, func);
1502 if (!d->access->backend_data)
1503 d->no_config_access = 1;
1504 d->backend_data = (void *)devinst;
1506 /* Parse device id part of devinst id and fill details into pci_dev. */
1508 fill_data_from_devinst_id(d, devinst_id);
1510 /* Retrieve hardware ids of devinst, parse them and fill details into pci_dev. */
1512 fill_data_from_hardware_ids(d, devinst, devinst_id);
1515 fill_resources(d, devinst, devinst_id);
1518 * Set parent field to cfgmgr32 parent devinst handle and backend_data field to current
1519 * devinst handle. At later stage in win32_cfgmgr32_scan() when all pci_dev
1520 * devices are linked, change every devinst handle by pci_dev.
1524 DEVINST parent_devinst;
1525 if (CM_Get_Parent(&parent_devinst, devinst, 0) != CR_SUCCESS)
1528 a->warning("Cannot retrieve parent handle for device %s: %s.", devinst_id, cr_strerror(cr));
1530 d->parent = (void *)parent_devinst;
1535 win32_cfgmgr32_scan(struct pci_access *a)
1537 ULONG devinst_id_list_size;
1538 PCHAR devinst_id_list;
1539 DEVINSTID_A devinst_id;
1543 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
1544 if (!resolve_cfgmgr32_functions())
1546 a->warning("Required cfgmgr32.dll functions are unavailable.");
1552 * Explicitly initialize size to zero as wine cfgmgr32 implementation does not
1553 * support this API but returns CR_SUCCESS without touching size argument.
1555 devinst_id_list_size = 0;
1556 cr = CM_Get_Device_ID_List_SizeA(&devinst_id_list_size, "PCI", CM_GETIDLIST_FILTER_ENUMERATOR);
1557 if (cr != CR_SUCCESS)
1559 a->warning("Cannot retrieve list of PCI devices: %s.", cr_strerror(cr));
1562 else if (devinst_id_list_size <= 1)
1564 a->warning("Cannot retrieve list of PCI devices: No device was found.");
1568 devinst_id_list = pci_malloc(a, devinst_id_list_size);
1569 cr = CM_Get_Device_ID_ListA("PCI", devinst_id_list, devinst_id_list_size, CM_GETIDLIST_FILTER_ENUMERATOR);
1570 if (cr != CR_SUCCESS)
1572 a->warning("Cannot retrieve list of PCI devices: %s.", cr_strerror(cr));
1573 pci_mfree(devinst_id_list);
1577 /* Register pci_dev for each cfgmgr32 devinst handle. */
1578 for (devinst_id = devinst_id_list; *devinst_id; devinst_id += strlen(devinst_id) + 1)
1579 scan_devinst_id(a, devinst_id);
1581 /* Fill all drivers. */
1585 /* Switch parent fields from cfgmgr32 devinst handle to pci_dev. */
1588 struct pci_dev *d1, *d2;
1589 for (d1 = a->devices; d1; d1 = d1->next)
1591 for (d2 = a->devices; d2; d2 = d2->next)
1592 if ((DEVINST)d1->parent == (DEVINST)d2->backend_data)
1596 d1->known_fields |= PCI_FILL_PARENT;
1600 /* devinst stored in ->backend_data is not needed anymore, clear it. */
1601 for (d = a->devices; d; d = d->next)
1602 d->backend_data = NULL;
1604 pci_mfree(devinst_id_list);
1608 win32_cfgmgr32_config(struct pci_access *a)
1610 pci_define_param(a, "win32.cfgmethod", "auto", "PCI config space access method");
1614 win32_cfgmgr32_detect(struct pci_access *a)
1616 ULONG devinst_id_list_size;
1619 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
1620 if (!resolve_cfgmgr32_functions())
1622 a->debug("Required cfgmgr32.dll functions are unavailable.");
1628 * Explicitly initialize size to zero as wine cfgmgr32 implementation does not
1629 * support this API but returns CR_SUCCESS without touching size argument.
1631 devinst_id_list_size = 0;
1632 cr = CM_Get_Device_ID_List_SizeA(&devinst_id_list_size, "PCI", CM_GETIDLIST_FILTER_ENUMERATOR);
1633 if (cr != CR_SUCCESS)
1635 a->debug("CM_Get_Device_ID_List_SizeA(\"PCI\"): %s.", cr_strerror(cr));
1638 else if (devinst_id_list_size <= 1)
1640 a->debug("CM_Get_Device_ID_List_SizeA(\"PCI\"): No device was found.");
1648 win32_cfgmgr32_fill_info(struct pci_dev *d, unsigned int flags)
1651 * All available flags were filled by win32_cfgmgr32_scan().
1652 * Filling more flags is possible only from config space.
1654 if (!d->access->backend_data)
1657 pci_generic_fill_info(d, flags);
1661 win32_cfgmgr32_read(struct pci_dev *d, int pos, byte *buf, int len)
1663 struct pci_access *a = d->access;
1664 struct pci_access *acfg = a->backend_data;
1665 struct pci_dev *dcfg = d->backend_data;
1668 return pci_emulated_read(d, pos, buf, len);
1671 d->backend_data = dcfg = pci_get_dev(acfg, d->domain, d->bus, d->dev, d->func);
1673 return pci_read_block(dcfg, pos, buf, len);
1677 win32_cfgmgr32_write(struct pci_dev *d, int pos, byte *buf, int len)
1679 struct pci_access *a = d->access;
1680 struct pci_access *acfg = a->backend_data;
1681 struct pci_dev *dcfg = d->backend_data;
1687 d->backend_data = dcfg = pci_get_dev(acfg, d->domain, d->bus, d->dev, d->func);
1689 return pci_write_block(dcfg, pos, buf, len);
1693 win32_cfgmgr32_cleanup_dev(struct pci_dev *d)
1695 struct pci_dev *dcfg = d->backend_data;
1702 win32_cfgmgr32_init(struct pci_access *a)
1704 char *cfgmethod = pci_get_param(a, "win32.cfgmethod");
1705 struct pci_access *acfg;
1707 if (strcmp(cfgmethod, "") == 0 ||
1708 strcmp(cfgmethod, "auto") == 0)
1710 acfg = pci_clone_access(a);
1711 acfg->method = PCI_ACCESS_AUTO;
1713 else if (strcmp(cfgmethod, "none") == 0 ||
1714 strcmp(cfgmethod, "win32-cfgmgr32") == 0)
1717 a->error("Write access requested but option win32.cfgmethod was not set.");
1722 int m = pci_lookup_method(cfgmethod);
1724 a->error("Option win32.cfgmethod is set to an unknown access method \"%s\".", cfgmethod);
1725 acfg = pci_clone_access(a);
1729 a->debug("Loading config space access method...\n");
1730 if (!pci_init_internal(acfg, PCI_ACCESS_WIN32_CFGMGR32))
1733 a->debug("Cannot find any working config space access method.\n");
1735 a->error("Write access requested but no usable access method found.");
1739 a->backend_data = acfg;
1743 win32_cfgmgr32_cleanup(struct pci_access *a)
1745 struct pci_access *acfg = a->backend_data;
1751 struct pci_methods pm_win32_cfgmgr32 = {
1752 .name = "win32-cfgmgr32",
1753 .help = "Win32 device listing via Configuration Manager",
1754 .config = win32_cfgmgr32_config,
1755 .detect = win32_cfgmgr32_detect,
1756 .init = win32_cfgmgr32_init,
1757 .cleanup = win32_cfgmgr32_cleanup,
1758 .scan = win32_cfgmgr32_scan,
1759 .fill_info = win32_cfgmgr32_fill_info,
1760 .read = win32_cfgmgr32_read,
1761 .write = win32_cfgmgr32_write,
1762 .cleanup_dev = win32_cfgmgr32_cleanup_dev,