forked from spddl/GoInterruptPolicy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winapi.go
133 lines (112 loc) · 4.02 KB
/
winapi.go
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
package main
import (
"syscall"
"unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
var (
modSetupapi = windows.NewLazyDLL("setupapi.dll")
procSetupDiGetClassDevsW = modSetupapi.NewProc("SetupDiGetClassDevsW")
)
func FindAllDevices() ([]Device, DevInfo) {
var allDevices []Device
handle, err := SetupDiGetClassDevs(nil, nil, 0, uint32(DIGCF_ALLCLASSES|DIGCF_PRESENT|DIGCF_PROFILE))
if err != nil {
panic(err)
}
var index = 0
for {
idata, err := SetupDiEnumDeviceInfo(handle, index)
if err != nil { // ERROR_NO_MORE_ITEMS
break
}
index++
dev := Device{
Idata: *idata,
}
val, err := SetupDiGetDeviceRegistryProperty(handle, idata, SPDRP_DEVICEDESC)
if err == nil {
if val.(string) == "" {
continue
}
dev.DeviceDesc = val.(string)
} else {
continue
}
valProp, err := GetDeviceProperty(handle, idata, DEVPKEY_PciRootBus_PCIExpressNativePMEControl)
if err == nil {
dev.InterruptTypeMap = Bits(btoi16(valProp))
}
valProp, err = GetDeviceProperty(handle, idata, DEVPKEY_PciRootBus_MaxMSILimit)
if err == nil {
dev.MaxMSILimit = btoi32(valProp)
}
val, err = SetupDiGetDeviceRegistryProperty(handle, idata, SPDRP_FRIENDLYNAME)
if err == nil {
dev.FriendlyName = val.(string)
}
val, err = SetupDiGetDeviceRegistryProperty(handle, idata, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME)
if err == nil {
dev.DevObjName = val.(string)
}
val, err = SetupDiGetDeviceRegistryProperty(handle, idata, SPDRP_LOCATION_INFORMATION)
if err == nil {
dev.LocationInformation = val.(string)
}
dev.reg, _ = SetupDiOpenDevRegKey(handle, idata, DICS_FLAG_GLOBAL, 0, DIREG_DEV, windows.KEY_SET_VALUE)
keyinfo, err := dev.reg.Stat()
if err == nil {
dev.LastChange = keyinfo.ModTime()
}
affinityPolicyKey, _ := registry.OpenKey(dev.reg, `Interrupt Management\Affinity Policy`, registry.QUERY_VALUE)
dev.DevicePolicy = GetDWORDuint32Value(affinityPolicyKey, "DevicePolicy") // REG_DWORD
dev.DevicePriority = GetDWORDuint32Value(affinityPolicyKey, "DevicePriority") // REG_DWORD
AssignmentSetOverrideByte := GetBinaryValue(affinityPolicyKey, "AssignmentSetOverride") // REG_BINARY
affinityPolicyKey.Close()
if len(AssignmentSetOverrideByte) != 0 {
AssignmentSetOverrideBytes := make([]byte, 8)
copy(AssignmentSetOverrideBytes, AssignmentSetOverrideByte)
dev.AssignmentSetOverride = Bits(btoi64(AssignmentSetOverrideBytes))
}
if dev.InterruptTypeMap != ZeroBit {
messageSignaledInterruptPropertiesKey, _ := registry.OpenKey(dev.reg, `Interrupt Management\MessageSignaledInterruptProperties`, registry.QUERY_VALUE)
dev.MessageNumberLimit = GetDWORDuint32Value(messageSignaledInterruptPropertiesKey, "MessageNumberLimit") // REG_DWORD https://docs.microsoft.com/de-de/windows-hardware/drivers/kernel/enabling-message-signaled-interrupts-in-the-registry
dev.MsiSupported = GetDWORDuint32Value(messageSignaledInterruptPropertiesKey, "MSISupported") // REG_DWORD
messageSignaledInterruptPropertiesKey.Close()
} else {
dev.MsiSupported = 2 // invalid
}
allDevices = append(allDevices, dev)
}
return allDevices, handle
}
func SetupDiGetClassDevs(classGuid *windows.GUID, enumerator *uint16, hwndParent uintptr, flags uint32) (handle DevInfo, err error) {
r0, _, e1 := syscall.SyscallN(procSetupDiGetClassDevsW.Addr(), uintptr(unsafe.Pointer(classGuid)), uintptr(unsafe.Pointer(enumerator)), uintptr(hwndParent), uintptr(flags))
handle = DevInfo(r0)
if handle == DevInfo(windows.InvalidHandle) {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetDeviceProperty(dis DevInfo, devInfoData *DevInfoData, devPropKey DEVPROPKEY) ([]byte, error) {
var propt, size uint32
buf := make([]byte, 16)
run := true
for run {
err := SetupDiGetDeviceProperty(dis, devInfoData, &devPropKey, &propt, &buf[0], uint32(len(buf)), &size, 0)
switch {
case size > uint32(len(buf)):
buf = make([]byte, size+16)
case err != nil:
return buf, err
default:
run = false
}
}
return buf, nil
}