forked from grayver/mi-vigem
-
Notifications
You must be signed in to change notification settings - Fork 13
/
hid.c
437 lines (381 loc) · 14.9 KB
/
hid.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* hid.c -- Routines for interacting with HID devices.
*/
#include "hid.h"
#include "utils.h"
#include <tchar.h>
#include <initguid.h>
#include <windows.h>
#include <hidsdi.h>
#include <setupapi.h>
#include <devpkey.h>
#include <cfgmgr32.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "hid.lib")
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "cfgmgr32.lib")
static BOOL got_hid_class = FALSE;
static GUID hid_class;
GUID hid_get_class()
{
if (!got_hid_class)
{
HidD_GetHidGuid(&hid_class);
got_hid_class = TRUE;
}
return hid_class;
}
struct hid_device_info *hid_enumerate(const LPTSTR *path_filters)
{
struct hid_device_info *root_dev = NULL;
struct hid_device_info *cur_dev = NULL;
GUID class_guid = hid_get_class();
SP_DEVINFO_DATA devinfo_data;
SP_DEVICE_INTERFACE_DATA device_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA *device_interface_detail_data = NULL;
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
DWORD required_size = 0;
DEVPROPTYPE prop_type;
LPTSTR desc_buffer = NULL;
LPWSTR desc_buffer_w = NULL;
memset(&devinfo_data, 0x0, sizeof(devinfo_data));
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
device_info_set = SetupDiGetClassDevs(&class_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (device_info_set == INVALID_HANDLE_VALUE)
{
return NULL;
}
DWORD device_index = 0;
while (SetupDiEnumDeviceInfo(device_info_set, device_index, &devinfo_data))
{
DWORD device_interface_index = 0;
while (SetupDiEnumDeviceInterfaces(device_info_set, &devinfo_data, &class_guid, device_interface_index, &device_interface_data))
{
SetupDiGetDeviceInterfaceDetail(device_info_set, &device_interface_data, NULL, 0, &required_size, NULL);
device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(required_size);
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (SetupDiGetDeviceInterfaceDetail(device_info_set, &device_interface_data, device_interface_detail_data, required_size, NULL, NULL))
{
BOOL matched = TRUE;
if (path_filters != NULL)
{
matched = FALSE;
for (const LPTSTR *pfilter = path_filters; *pfilter != NULL; pfilter++)
{
if (_tcsistr(device_interface_detail_data->DevicePath, *pfilter) != NULL)
{
matched = TRUE;
break;
}
}
}
if (matched)
{
desc_buffer = NULL;
if (SetupDiGetDevicePropertyW(device_info_set, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&prop_type, NULL, 0, &required_size, 0))
{
desc_buffer_w = (LPWSTR)malloc(required_size);
memset(desc_buffer_w, 0, required_size);
SetupDiGetDevicePropertyW(device_info_set, &devinfo_data, &DEVPKEY_Device_BusReportedDeviceDesc,
&prop_type, (PBYTE)desc_buffer_w, required_size, NULL, 0);
#ifdef UNICODE
desc_buffer = desc_buffer_w;
#else
int desc_buffer_size = WideCharToMultiByte(CP_ACP, 0, desc_buffer_w, -1, desc_buffer, 0, NULL, NULL);
desc_buffer = (LPSTR)malloc(desc_buffer_size);
WideCharToMultiByte(CP_ACP, 0, desc_buffer_w, -1, desc_buffer, desc_buffer_size, NULL, NULL);
free(desc_buffer_w);
#endif /* UNICODE */
}
if (desc_buffer == NULL || _tcslen(desc_buffer) == 0)
{
if (desc_buffer != NULL)
{
free(desc_buffer);
}
if (SetupDiGetDeviceRegistryProperty(device_info_set, &devinfo_data, SPDRP_DEVICEDESC,
NULL, NULL, 0, &required_size) ||
GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
desc_buffer = (LPTSTR)malloc(required_size);
memset(desc_buffer, 0, required_size);
SetupDiGetDeviceRegistryProperty(device_info_set, &devinfo_data, SPDRP_DEVICEDESC,
NULL, (PBYTE)desc_buffer, required_size, NULL);
}
}
struct hid_device_info *dev = (struct hid_device_info *)malloc(sizeof(struct hid_device_info));
dev->path = (LPTSTR)malloc((_tcslen(device_interface_detail_data->DevicePath) + 1) * sizeof(TCHAR));
_tcscpy(dev->path, device_interface_detail_data->DevicePath);
dev->description = desc_buffer;
dev->next = NULL;
if (root_dev == NULL)
{
root_dev = dev;
}
else
{
cur_dev->next = dev;
}
cur_dev = dev;
}
}
free(device_interface_detail_data);
device_interface_index++;
}
device_index++;
}
SetupDiDestroyDeviceInfoList(device_info_set);
return root_dev;
}
BOOL hid_reenable_device(LPTSTR path)
{
GUID class_guid = hid_get_class();
SP_DEVINFO_DATA devinfo_data;
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
DWORD required_size = 0;
LPWSTR path_w;
LPTSTR inst_id = NULL;
memset(&devinfo_data, 0x0, sizeof(devinfo_data));
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
#ifdef UNICODE
path_w = path;
#else
int path_length = strlen(path);
path_w = malloc((path_length + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, path, -1, path_w, path_length + 1);
#endif /* UNICODE */
DEVPROPTYPE prop_type;
CM_Get_Device_Interface_PropertyW(path_w, &DEVPKEY_Device_InstanceId, &prop_type, NULL, &required_size, 0);
LPWSTR inst_id_w = (LPWSTR)malloc(required_size);
if (CM_Get_Device_Interface_PropertyW(path_w, &DEVPKEY_Device_InstanceId, &prop_type, (PBYTE)inst_id_w, &required_size, 0) != CR_SUCCESS)
{
free(inst_id_w);
return FALSE;
}
#ifdef UNICODE
inst_id = inst_id_w;
#else
free(path_w);
int inst_id_size = WideCharToMultiByte(CP_ACP, 0, inst_id_w, -1, inst_id, 0, NULL, NULL);
inst_id = (LPSTR)malloc(inst_id_size);
WideCharToMultiByte(CP_ACP, 0, inst_id_w, -1, inst_id, inst_id_size, NULL, NULL);
free(inst_id_w);
#endif /* UNICODE */
device_info_set = SetupDiGetClassDevs(&class_guid, inst_id, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (device_info_set == INVALID_HANDLE_VALUE)
{
free(inst_id);
return FALSE;
}
if (!SetupDiEnumDeviceInfo(device_info_set, 0, &devinfo_data) || SetupDiEnumDeviceInfo(device_info_set, 1, &devinfo_data))
{
free(inst_id);
SetupDiDestroyDeviceInfoList(device_info_set);
return FALSE;
}
SP_PROPCHANGE_PARAMS pc_params =
{
.ClassInstallHeader =
{
.cbSize = sizeof(SP_CLASSINSTALL_HEADER),
.InstallFunction = DIF_PROPERTYCHANGE},
.StateChange = DICS_DISABLE,
.Scope = DICS_FLAG_GLOBAL,
.HwProfile = 0};
BOOL res;
res = SetupDiSetClassInstallParams(device_info_set, &devinfo_data, (PSP_CLASSINSTALL_HEADER)&pc_params,
sizeof(SP_PROPCHANGE_PARAMS));
res = res && SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, device_info_set, &devinfo_data);
pc_params.StateChange = DICS_ENABLE;
res = res && SetupDiSetClassInstallParams(device_info_set, &devinfo_data, (PSP_CLASSINSTALL_HEADER)&pc_params,
sizeof(SP_PROPCHANGE_PARAMS));
res = res && SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, device_info_set, &devinfo_data);
free(inst_id);
SetupDiDestroyDeviceInfoList(device_info_set);
return res;
}
BOOL check_vendor_and_product(LPTSTR path, USHORT vendor_id, USHORT product_id)
{
HANDLE dev_handle = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (dev_handle != INVALID_HANDLE_VALUE)
{
BOOL matched = FALSE;
HIDD_ATTRIBUTES attributes =
{
.Size = sizeof(HIDD_ATTRIBUTES)};
if (HidD_GetAttributes(dev_handle, &attributes))
{
matched = (vendor_id == 0x0 || attributes.VendorID == vendor_id) && (product_id == 0x0 || attributes.ProductID == product_id);
}
CloseHandle(dev_handle);
return matched;
}
else
{
return FALSE;
}
}
void hid_free_device_info(struct hid_device_info *device_info)
{
free(device_info->description);
free(device_info->path);
free(device_info);
}
struct hid_device *hid_open_device(LPTSTR path, BOOL access_rw, BOOL shared)
{
DWORD desired_access = access_rw ? (GENERIC_WRITE | GENERIC_READ) : 0;
DWORD share_mode = shared ? (FILE_SHARE_READ | FILE_SHARE_WRITE) : 0;
SECURITY_ATTRIBUTES security =
{
.nLength = sizeof(SECURITY_ATTRIBUTES),
.lpSecurityDescriptor = NULL,
.bInheritHandle = TRUE};
HANDLE handle = CreateFile(path, desired_access, share_mode, &security, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (handle == INVALID_HANDLE_VALUE)
{
return NULL;
}
PHIDP_PREPARSED_DATA pp_data = NULL;
if (!HidD_GetPreparsedData(handle, &pp_data))
{
CloseHandle(handle);
return NULL;
}
HIDP_CAPS caps;
if (HidP_GetCaps(pp_data, &caps) != HIDP_STATUS_SUCCESS)
{
HidD_FreePreparsedData(pp_data);
CloseHandle(handle);
return NULL;
}
struct hid_device *dev = (struct hid_device *)malloc(sizeof(struct hid_device));
dev->path = (LPTSTR)malloc((_tcslen(path) + 1) * sizeof(TCHAR));
_tcscpy(dev->path, path);
dev->handle = handle;
dev->read_pending = FALSE;
dev->input_report_size = caps.InputReportByteLength;
dev->output_report_size = caps.OutputReportByteLength;
dev->feature_report_size = caps.FeatureReportByteLength;
dev->input_buffer = (BYTE *)malloc(caps.InputReportByteLength);
dev->output_buffer = (BYTE *)malloc(caps.OutputReportByteLength);
dev->feature_buffer = (BYTE *)malloc(caps.FeatureReportByteLength);
HidD_FreePreparsedData(pp_data);
memset(&dev->input_ol, 0, sizeof(OVERLAPPED));
dev->input_ol.hEvent = CreateEvent(&security, FALSE, FALSE, NULL);
memset(&dev->output_ol, 0, sizeof(OVERLAPPED));
dev->output_ol.hEvent = CreateEvent(&security, FALSE, FALSE, NULL);
return dev;
}
INT hid_get_input_report(struct hid_device *device, DWORD timeout)
{
DWORD bytes_read = 0;
HANDLE ev = device->input_ol.hEvent;
if (!device->read_pending)
{
device->read_pending = TRUE;
memset(device->input_buffer, 0, device->input_report_size);
ResetEvent(ev);
if (!ReadFile(device->handle, device->input_buffer, device->input_report_size, &bytes_read, &device->input_ol))
{
if (GetLastError() != ERROR_IO_PENDING)
{
CancelIo(device->handle);
device->read_pending = FALSE;
return -1;
}
}
}
if (timeout >= 0)
{
if (WaitForSingleObject(ev, timeout) != WAIT_OBJECT_0)
{
/* There was no data this time. Return zero bytes available,
but leave the Overlapped I/O running. */
return 0;
}
}
/* Either WaitForSingleObject() told us that ReadFile has completed, or
we are in non-blocking mode. Get the number of bytes read. The actual
data has been copied to the data[] array which was passed to ReadFile(). */
if (GetOverlappedResult(device->handle, &device->input_ol, &bytes_read, TRUE))
{
device->read_pending = FALSE;
return bytes_read;
}
device->read_pending = FALSE;
return -1;
}
INT hid_send_output_report(struct hid_device *device, const void *data, size_t length, DWORD timeout)
{
DWORD bytes_written = 0;
HANDLE ev = device->output_ol.hEvent;
if (!device->write_pending)
{
device->write_pending = TRUE;
memset(device->output_buffer, 0x0, device->output_report_size);
memmove(device->output_buffer, data, length > device->output_report_size ? device->output_report_size : length);
ResetEvent(ev);
if (!WriteFile(device->handle, device->output_buffer, device->output_report_size, &bytes_written, &device->output_ol))
{
if (GetLastError() != ERROR_IO_PENDING)
{
CancelIo(device->handle);
device->write_pending = FALSE;
return -1;
}
}
}
if (timeout >= 0)
{
if (WaitForSingleObject(ev, timeout) != WAIT_OBJECT_0)
{
/* There was no data this time. Return zero bytes written,
but leave the Overlapped I/O running. */
return 0;
}
}
/* Either WaitForSingleObject() told us that WriteFile has completed, or
we are in non-blocking mode. Get the number of bytes written. The actual
data was passed to WriteFile(). */
if (GetOverlappedResult(device->handle, &device->output_ol, &bytes_written, TRUE))
{
device->write_pending = FALSE;
return bytes_written;
}
device->write_pending = FALSE;
return -1;
}
INT hid_send_feature_report(struct hid_device *device, const void *data, size_t length)
{
if (length <= device->feature_report_size)
{
memset(device->feature_buffer, 0, device->feature_report_size);
memmove(device->feature_buffer, data, length);
}
else
{
memmove(device->feature_buffer, data, device->feature_report_size);
}
if (HidD_SetFeature(device->handle, (PVOID)device->feature_buffer, device->feature_report_size))
{
return length < device->feature_report_size ? length : device->feature_report_size;
}
return -1;
}
void hid_close_device(struct hid_device *device)
{
CancelIoEx(device->handle, NULL);
CloseHandle(device->input_ol.hEvent);
CloseHandle(device->output_ol.hEvent);
CloseHandle(device->handle);
}
void hid_free_device(struct hid_device *device)
{
free(device->path);
free(device->input_buffer);
free(device->output_buffer);
free(device->feature_buffer);
free(device);
}