-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuhwi_macos.c
218 lines (172 loc) · 8.02 KB
/
uhwi_macos.c
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
//
// Copyright (C) 2023 Universe-OS
// Copyright (C) 2023 Tim K. <timk@xfen.page>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugin.h>
#include <IOKit/usb/IOUSBLib.h>
#include "uhwi.h"
extern uhwi_errno_t uhwi_last_errno;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_12_0
// macOS 12.0 renamed master port to main
#define kIOMainPortDefault kIOMasterPortDefault
#endif
#define CFSTR_FROM_CSTR_ASCII(key) \
CFStringCreateWithCString(kCFAllocatorDefault, key, \
kCFStringEncodingASCII)
void uhwi_strncpy_macos_dev_name_cstr(const uhwi_dev_t type,
const io_service_t dvv,
char* buf, const size_t max) {
if (type == UHWI_DEV_NULL)
return; // makes sense
// detect what kind of key applies to the device we are working with (the
// key that contains a user-friendly C string rendition of the device's
// product string)
const char* key = (type == UHWI_DEV_USB) ? kUSBProductString : "IOName";
// try to obtain that key's value
CFStringRef nmp = CFSTR_FROM_CSTR_ASCII(key);
CFTypeRef raw = IORegistryEntryCreateCFProperty(dvv, nmp, kCFAllocatorDefault,
0);
// copy the resulting value, if valid, to the provided C string buffer
if (raw && CFGetTypeID(raw) == CFStringGetTypeID())
CFStringGetCString(raw, buf, max, kCFStringEncodingUTF8);
// clean up
CFRelease(nmp);
}
uhwi_id_t uhwi_get_macos_pci_param(const io_service_t pci,
const char* key,
const uhwi_id_t dfv) {
SInt16 result = (SInt16)dfv;
CFStringRef kcf = CFSTR_FROM_CSTR_ASCII(key);
CFTypeRef raw = IORegistryEntryCreateCFProperty(pci, kcf, kCFAllocatorDefault,
0);
if (raw) {
if (CFGetTypeID(raw) == CFNumberGetTypeID())
CFNumberGetValue(raw, kCFNumberSInt16Type, &result);
else if (CFGetTypeID(raw) == CFDataGetTypeID())
result = (SInt16)(*((long*)(CFDataGetBytePtr(raw))));
}
CFRelease(kcf);
return (uhwi_id_t)result;
}
uhwi_dev* uhwi_get_macos_devs(const uhwi_dev_t type, uhwi_dev** lpp) {
if (type == UHWI_DEV_NULL)
return NULL; // must be a specific device type request
uhwi_dev* first = NULL;
uhwi_dev* last = NULL;
uhwi_last_errno = UHWI_ERRNO_OK;
// make a class matcher dictionary thing
const char* mcn = (type == UHWI_DEV_USB) ? "IOUSBDevice" : "IOPCIDevice";
CFMutableDictionaryRef matcher = IOServiceMatching(mcn);
if (!matcher) {
uhwi_last_errno = UHWI_ERRNO_IOKIT_NO_MEM;
return NULL;
}
// obtain all IOKit IOService instances (these are basically device-representing
// objects which are just typedef-ed mach_port_t-s)
io_iterator_t iter = 0;
kern_return_t err = IOServiceGetMatchingServices(kIOMainPortDefault,
matcher,
&iter);
if (err != KERN_SUCCESS) {
// clean up and fail
CFRelease(matcher);
uhwi_last_errno = UHWI_ERRNO_IOKIT_SERVICE;
return NULL;
}
// iterate through all the detected devices
io_service_t dvv = 0;
while (1) {
dvv = IOIteratorNext(iter);
if (dvv) {
uhwi_dev* current = NULL;
if (type == UHWI_DEV_USB) {
//
// big thx to https://gist.github.com/thinkski/0ebc53ab4989d904a04c
//
// honestly, I can barely understand what kind of sourcery does this
// half-baked API even do...
//
// ..guess what? Apple didn't bother to shit out even a brief des-
// cription of what all these functions and triple-pointers do!!
//
// see for yourself:
// https://developer.apple.com/documentation/iokit/1412429-iocreateplugininterfaceforservic?language=objc
//
// I love the Mac, but man this API is so mediocre
//
IOCFPlugInInterface** ifdp = NULL;
IOUSBDeviceInterface** dvdp = NULL;
// this is literally something we will never use in the code,
// but this has to be here since we can't just pass NULL as
// the final argument to IOCreatePlugInInterfaceForService
// as it would crash in that case
SInt32 state = 0;
err = IOCreatePlugInInterfaceForService(dvv, kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&ifdp, &state);
if (err != kIOReturnSuccess || !ifdp)
continue;
HRESULT rsl = (*ifdp)->QueryInterface(ifdp,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID650),
(LPVOID*)&dvdp);
if (rsl != 0 || !dvdp)
continue;
// finally, obtain device vendor and product ID after the dark
// magic performed above
current = malloc(sizeof(uhwi_dev));
memset(current, 0, sizeof(uhwi_dev));
(*dvdp)->GetDeviceVendor(dvdp, ¤t->vendor);
(*dvdp)->GetDeviceProduct(dvdp, ¤t->device);
} else if (type == UHWI_DEV_PCI) {
current = malloc(sizeof(uhwi_dev));
memset(current, 0, sizeof(uhwi_dev));
// at least with PCI it's more or less straightforward
current->vendor = uhwi_get_macos_pci_param(dvv, "vendor-id", 0);
current->device = uhwi_get_macos_pci_param(dvv, "device-id", 0);
current->subvendor = uhwi_get_macos_pci_param(dvv, "subsystem-vendor-id", 0);
current->subdevice = uhwi_get_macos_pci_param(dvv, "subsystem-id", 0);
}
// add our UHWI device structure to the linked list if it is valid
if (current) {
current->type = type;
// try to obtain device name C string, if possible
uhwi_strncpy_macos_dev_name_cstr(type, dvv, current->name,
UHWI_DEV_NAME_MAX_LEN - 1);
if (last)
last->next = current;
else
first = current;
last = current;
}
// clean up the device object reference port thing
IOObjectRelease(dvv);
} else
break;
}
// clean up
IOObjectRelease(iter);
// provide the user with the last UHWI device structure pointer, if requested
if (lpp)
(*lpp) = last;
return first;
}