-
Notifications
You must be signed in to change notification settings - Fork 39
/
lusbk_device_list.c
1442 lines (1182 loc) · 47.2 KB
/
lusbk_device_list.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!********************************************************************
libusbK - Multi-driver USB library.
Copyright (C) 2012 Travis Lee Robinson. All Rights Reserved.
libusb-win32.sourceforge.net
Development : Travis Lee Robinson (libusbdotnet@gmail.com)
Testing : Xiaofan Chen (xiaofanc@gmail.com)
At the discretion of the user of this library, this software may be
licensed under the terms of the GNU Public License v3 or a BSD-Style
license as outlined in the following files:
* LICENSE-gpl3.txt
* LICENSE-bsd.txt
License files are located in a license folder at the root of source and
binary distributions.
********************************************************************!*/
#include "lusbk_private.h"
#include "lusbk_handles.h"
#include "lusb_defdi_guids.h"
#include "lusbk_linked_list.h"
#include <setupapi.h>
// warning C4127: conditional expression is constant.
#pragma warning(disable: 4127)
#define mLst_DL_Match_SymbolicLink(PatternEL, MatchEL) (PathMatchSpec((MatchEL)->Public.SymbolicLink, (PatternEL)->Public.SymbolicLink)?0:-1)
#define KEY_DEVICECLASSES "SYSTEM\\CurrentControlSet\\Control\\DeviceClasses"
#define mLst_Get_InterfaceDetail(mRegEnumParamsPtr, mhDevInfo_Interface, mOutDevInfoDataPtr, mOutDevicePathLen, mErrorAction)do { \
/* This aligns the 'DevicePath' in PSP_DEVICE_INTERFACE_DETAIL_DATA to mRegEnumParamsPtr->TempItem->DevicePath */ \
PSP_DEVICE_INTERFACE_DETAIL_DATA m_pDevInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)((&(mRegEnumParamsPtr)->TempItem->DevicePath[0]) - sizeof(DWORD)); \
m_pDevInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); \
mOutDevicePathLen = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + sizeof((mRegEnumParamsPtr)->TempItem->DevicePath) - 2; \
\
/* Get the DevicePath/SymbolicLink */ \
if (!SetupDiGetDeviceInterfaceDetailA( \
mhDevInfo_Interface, \
&(mRegEnumParamsPtr)->DevInterfaceData, \
m_pDevInterfaceDetailData, \
mOutDevicePathLen, \
&mOutDevicePathLen, \
mOutDevInfoDataPtr)) \
{ \
USBERRN("SetupDiGetDeviceInterfaceDetail Failed. ErrorCode:%08Xh", GetLastError()); \
{mErrorAction;} \
} \
\
memset((mRegEnumParamsPtr)->TempItem->DevicePath - sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA), 0, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)); \
mOutDevicePathLen = (DWORD)strlen((mRegEnumParamsPtr)->TempItem->DevicePath); \
memcpy((mRegEnumParamsPtr)->TempItem->SymbolicLink, (mRegEnumParamsPtr)->TempItem->DevicePath, mOutDevicePathLen); \
(mRegEnumParamsPtr)->TempItem->DevicePath[mOutDevicePathLen] = (CHAR)0; \
(mRegEnumParamsPtr)->TempItem->SymbolicLink[mOutDevicePathLen] = (CHAR)0; \
} \
while(0)
#define mLst_HandleConnectScenario(mRegEnumParamsPtr, mErrorAction)do { \
if ((mRegEnumParamsPtr)->DevInterfaceData.Flags & SPINT_REMOVED) \
{ \
mErrorAction; \
} \
/* If we are not including disconnected devices and the driver has not enabled the interface guid for this devices then skip it. */ \
(mRegEnumParamsPtr)->TempItem->Connected = ((mRegEnumParamsPtr)->DevInterfaceData.Flags & SPINT_ACTIVE) ? TRUE : FALSE; \
if ((mRegEnumParamsPtr)->TempItem->Connected == FALSE && !((mRegEnumParamsPtr)->Flags & KLST_FLAG_INCLUDE_DISCONNECT)) \
{ \
mErrorAction; \
} \
} \
while(0)
typedef struct _KUSB_ENUM_REGKEY_PARAMS
{
// assigned by LstK_Init, copied by sub enumeration routines
// (global)
PKLST_HANDLE_INTERNAL DeviceList;
// Heap used for device info allocation
HANDLE Heap;
KLST_FLAG Flags;
KLST_PATTERN_MATCH* PatternMatch;
GUID DevInterfaceGuid;
GUID ClassGuid;
// optional
PVOID Context;
DWORD ErrorCode;
struct
{
BOOL DevInterfaceGuid;
BOOL ClassGuid;
} Exclusive;
KLST_DEVINFO_HANDLE TempItem;
// SetupAPI
HDEVINFO DevInfoSet;
SP_DEVINFO_DATA DevInfoData;
SP_DEVICE_INTERFACE_DATA DevInterfaceData;
DWORD DigcFlags;
} KUSB_ENUM_REGKEY_PARAMS;
typedef BOOL ENUM_REGKEY_DELEGATE (LPCSTR Name, KUSB_ENUM_REGKEY_PARAMS* RegEnumParams);
typedef ENUM_REGKEY_DELEGATE* PENUM_REGKEY_DELEGATE;
typedef struct _KLST_SYNC_CONTEXT
{
PKLST_HANDLE_INTERNAL Master;
PKLST_HANDLE_INTERNAL Slave;
KLST_SYNC_FLAG SyncFlags;
} KLST_SYNC_CONTEXT;
typedef struct _SERVICE_DRVID_MAP
{
INT DriverID;
LPCSTR* MapNames;
}* PSERVICE_DRVID_MAP, SERVICE_DRVID_MAP;
typedef struct _DEV_INTF_VALUENAME_MAP
{
LPCSTR ValueName;
DWORD RegValueType;
}* PDEV_INTF_VALUENAME_MAP, DEV_INTF_VALUENAME_MAP;
static LPCSTR DrvIdNames[8] = {"libusbK", "libusb0", "WinUSB", "libusb0 filter", "Unknown", "Unknown", "Unknown"};
static LPCSTR lusb0_Services[] = {"LIBUSB0", NULL};
static LPCSTR lusbk_Services[] = {"LIBUSBK", NULL};
static LPCSTR wusb_Services[] = {"WINUSB", NULL};
static LPCSTR lusb0_FilterDevGuidNames[] =
{
DEFINE_TO_STR(_DefLibusb0FilterGuid),
NULL
};
static LPCSTR lusb0_DevGuidNames[] =
{
DEFINE_TO_STR(_DefLibusb0DeviceGuid),
NULL
};
static LPCSTR lusbK_DevGuidNames[] =
{
DEFINE_TO_STR(_DefLibusbKDeviceGuid),
NULL
};
#define mLst_Assign_DrvId_From_Map(mOutDrvId, mInDrvMapValueToFind, mInServiceDrvIdMap) { \
PSERVICE_DRVID_MAP _serviceMap = (PSERVICE_DRVID_MAP)mInServiceDrvIdMap; \
while((mOutDrvId) == -1 && _serviceMap->DriverID != -1) \
{ \
LPCSTR* _mapName = _serviceMap->MapNames; \
while(*_mapName) \
{ \
if (_stricmp((mInDrvMapValueToFind), *_mapName) == 0) \
{ \
(mOutDrvId) = _serviceMap->DriverID; \
break; \
} \
_mapName++; \
} \
_serviceMap++; \
} \
} \
static const SERVICE_DRVID_MAP DrvIdMap_Services[] =
{
{KUSB_DRVID_LIBUSBK, lusbk_Services},
{KUSB_DRVID_LIBUSB0, lusb0_Services},
{KUSB_DRVID_WINUSB, wusb_Services},
{ -1, NULL}
};
#define mLst_Assign_DrvId_From_Service(mOutDrvId, mInServiceToFind) mLst_Assign_DrvId_From_Map(mOutDrvId, mInServiceToFind, DrvIdMap_Services)
static const SERVICE_DRVID_MAP DevGuidDrvIdMap[] =
{
{KUSB_DRVID_LIBUSBK, lusbK_DevGuidNames},
{KUSB_DRVID_LIBUSB0, lusb0_DevGuidNames},
{KUSB_DRVID_LIBUSB0_FILTER, lusb0_FilterDevGuidNames},
{ -1, NULL}
};
#define mLst_Assign_DrvId_From_InterfaceGuid(mOutDrvId, mInInterFaceGuidToFind) mLst_Assign_DrvId_From_Map(mOutDrvId, mInInterFaceGuidToFind, DevGuidDrvIdMap)
static BOOL KUSB_API l_DevEnum_Free_All(
__in KLST_HANDLE DeviceList,
__in KLST_DEVINFO_HANDLE DeviceInfo,
__in KLST_SYNC_CONTEXT* Context)
{
UNREFERENCED_PARAMETER(Context);
LstK_DetachInfo(DeviceList, DeviceInfo);
LstK_FreeInfo(DeviceInfo);
return TRUE;
}
static BOOL KUSB_API l_DevEnum_Clone_All(
__in KLST_HANDLE SrcDeviceList,
__in KLST_DEVINFO_HANDLE DeviceInfo,
__in KLST_HANDLE DstDeviceList)
{
KLST_DEVINFO_HANDLE ClonedDevInfo = NULL;
UNREFERENCED_PARAMETER(SrcDeviceList);
if (LstK_CloneInfo(DeviceInfo, &ClonedDevInfo))
{
LstK_AttachInfo(DstDeviceList, ClonedDevInfo);
return TRUE;
}
return FALSE;
}
static void KUSB_API Cleanup_DeviceList(__in PKLST_HANDLE_INTERNAL handle)
{
PoolHandle_Dead_LstK(handle);
LstK_Enumerate((KLST_HANDLE)handle, l_DevEnum_Free_All, NULL);
}
static void KUSB_API Cleanup_DevInfo(__in PKLST_DEVINFO_HANDLE_INTERNAL handle)
{
PoolHandle_Dead_LstInfoK(handle);
if (handle->Heap)
{
HeapFree(handle->Heap, 0, handle->DevInfoEL);
handle->DevInfoEL = NULL;
handle->Heap = NULL;
}
else
{
USBERRN("BugCheck! handle->Heap == NULL");
Mem_Free(&handle->DevInfoEL);
}
}
static BOOL KUSB_API l_DevEnum_SyncPrep(
__in KLST_HANDLE DeviceList,
__in KLST_DEVINFO_HANDLE DeviceInfo,
__in KLST_SYNC_CONTEXT* Context)
{
UNREFERENCED_PARAMETER(DeviceList);
UNREFERENCED_PARAMETER(Context);
DeviceInfo->SyncFlags = KLST_SYNC_FLAG_NONE;
return TRUE;
}
static BOOL KUSB_API l_DevEnum_Sync_Master(
__in KLST_HANDLE DeviceList,
__in KLST_DEVINFO_HANDLE DeviceInfo,
__in KLST_SYNC_CONTEXT* Context)
{
PKLST_DEVINFO_EL masterDevInfo = (PKLST_DEVINFO_EL)DeviceInfo;
PKLST_DEVINFO_EL slaveDevInfo;
UNREFERENCED_PARAMETER(DeviceList);
// Skip elements already processed by previous sync operations.
if (masterDevInfo->Public.SyncFlags != KLST_SYNC_FLAG_NONE) return TRUE;
DL_SEARCH(Context->Slave->head, slaveDevInfo, masterDevInfo, mLst_DL_Match_SymbolicLink);
if (slaveDevInfo)
{
// This element exists in the slave and master list.
memcpy(masterDevInfo->Public.DevicePath, slaveDevInfo->Public.DevicePath, KLST_STRING_MAX_LEN);
if (slaveDevInfo->Public.Connected != masterDevInfo->Public.Connected)
{
// Connected/Disconnected.
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_CONNECT_CHANGE & Context->SyncFlags);
masterDevInfo->Public.Connected = slaveDevInfo->Public.Connected;
if (slaveDevInfo->Public.Connected)
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_ADDED & Context->SyncFlags);
else
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_REMOVED & Context->SyncFlags);
}
else
{
// Unchanged.
masterDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_UNCHANGED;
}
}
else
{
// This element exists in the master list only.
if (masterDevInfo->Public.Connected)
{
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_REMOVED & Context->SyncFlags);
if (!masterDevInfo->Public.SyncFlags) masterDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_UNCHANGED;
masterDevInfo->Public.Connected = FALSE;
}
else
{
masterDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_UNCHANGED;
}
}
return TRUE;
}
static BOOL KUSB_API l_DevEnum_Sync_Slave(
__in KLST_HANDLE DeviceList,
__in KLST_DEVINFO_HANDLE DeviceInfo,
__in KLST_SYNC_CONTEXT* Context)
{
PKLST_DEVINFO_EL slaveDevInfo = (PKLST_DEVINFO_EL)DeviceInfo;
PKLST_DEVINFO_EL masterDevInfo;
UNREFERENCED_PARAMETER(DeviceList);
DL_SEARCH(Context->Master->head, masterDevInfo, slaveDevInfo, mLst_DL_Match_SymbolicLink);
if (masterDevInfo)
{
// This element exists on both lists.
// Skip elements already processed by previous sync operations.
if (masterDevInfo->Public.SyncFlags != KLST_SYNC_FLAG_NONE) return TRUE;
// always use the new DevicePath from the slave list
memcpy(masterDevInfo->Public.DevicePath, slaveDevInfo->Public.DevicePath, KLST_STRING_MAX_LEN);
if (slaveDevInfo->Public.Connected != masterDevInfo->Public.Connected)
{
// Connected/Disconnected.
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_CONNECT_CHANGE & Context->SyncFlags);
masterDevInfo->Public.Connected = slaveDevInfo->Public.Connected;
if (slaveDevInfo->Public.Connected)
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_ADDED & Context->SyncFlags);
else
masterDevInfo->Public.SyncFlags |= (KLST_SYNC_FLAG_REMOVED & Context->SyncFlags);
}
else
{
// Unchanged.
masterDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_UNCHANGED;
}
}
else
{
// This element exists in the slave but not in the master
if ((KLST_SYNC_FLAG_ADDED & Context->SyncFlags))
{
// Move it to the master list
LstK_DetachInfo(DeviceList, (KLST_DEVINFO_HANDLE)slaveDevInfo);
LstK_AttachInfo((KLST_HANDLE)Context->Master, (KLST_DEVINFO_HANDLE)slaveDevInfo);
// Update 'SyncFlags'
if (slaveDevInfo->Public.Connected)
slaveDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_ADDED;
else
slaveDevInfo->Public.SyncFlags = KLST_SYNC_FLAG_UNCHANGED;
}
}
return TRUE;
}
static BOOL l_Alloc_DevInfo(_out PKLST_DEVINFO_HANDLE_INTERNAL* handleRef, _inopt HANDLE Heap)
{
PKLST_DEVINFO_HANDLE_INTERNAL handle;
*handleRef = NULL;
handle = PoolHandle_Acquire_LstInfoK(Cleanup_DevInfo);
ErrorNoSetAction(!IsHandleValid(handle), return FALSE, "->PoolHandle_Acquire_LstInfoK");
if (Heap)
{
handle->DevInfoEL = HeapAlloc(Heap, HEAP_ZERO_MEMORY, sizeof(*handle->DevInfoEL));
handle->Heap = Heap;
}
else
{
handle->DevInfoEL = Mem_Alloc(sizeof(*handle->DevInfoEL));
handle->Heap = AllK->HeapDynamic;
}
ErrorMemory(!IsHandleValid(handle->DevInfoEL), Error);
handle->DevInfoEL->DevInfoHandle = handle;
*handleRef = handle;
return TRUE;
Error:
if (handle) PoolHandle_Dec_LstInfoK(handle);
return FALSE;
}
static DWORD l_Build_AddElement(
KUSB_ENUM_REGKEY_PARAMS* RegEnumParams,
PKLST_DEVINFO_EL* clonedDevInfo)
{
PKLST_DEVINFO_HANDLE_INTERNAL clonedHandle = NULL;
*clonedDevInfo = NULL;
if (!l_Alloc_DevInfo(&clonedHandle, RegEnumParams->Heap)) goto Error;
memcpy(&clonedHandle->DevInfoEL->Public, RegEnumParams->TempItem, sizeof(clonedHandle->DevInfoEL->Public));
// LstInfoK + 1
PoolHandle_Inc_LstInfoK(clonedHandle);
clonedHandle->DevInfoEL->DevListHandle = RegEnumParams->DeviceList;
clonedHandle->DevInfoEL->DevInfoHandle = clonedHandle;
DL_APPEND(RegEnumParams->DeviceList->head, clonedHandle->DevInfoEL);
*clonedDevInfo = clonedHandle->DevInfoEL;
return ERROR_SUCCESS;
Error:
return GetLastError();
}
static void l_Build_Common_Info(__in PKLST_DEVINFO_EL devItem)
{
PKLST_DEV_COMMON_INFO commonInfo = &devItem->Public.Common;
CHAR devID[KLST_STRING_MAX_LEN];
PCHAR chLast;
PCHAR chNext;
ULONG pos = 0;
commonInfo->MI = UINT_MAX;
commonInfo->Vid = UINT_MAX;
commonInfo->Pid = UINT_MAX;
strcpy_s(devID, sizeof(devID), devItem->Public.DeviceID);
_strupr_s(devID, sizeof(devID));
chLast = chNext = devID;
while(chLast && chLast[0])
{
if ((chNext = strchr(chNext, '\\')) != NULL)
{
*chNext = '\0';
chNext = &chNext[1];
}
switch(pos)
{
case 0: // USB
break;
case 1: // VID_%04X&PID_%04X
if (sscanf_s(chLast, "VID_%04X&PID_%04X&MI_%02X", &commonInfo->Vid, &commonInfo->Pid, &commonInfo->MI) < 2)
USBWRNN("Failed scanning vid/pid into common info.");
break;
case 2: // InstanceID
strcpy_s(commonInfo->InstanceID, sizeof(commonInfo->InstanceID), chLast);
break;
default:
USBWRNN("Unknown device instance id element: %s.", chLast);
break;
}
pos++;
chLast = chNext;
}
if (commonInfo->Vid != UINT_MAX)
commonInfo->Vid &= 0xFFFF;
if (commonInfo->Pid != UINT_MAX)
commonInfo->Pid &= 0xFFFF;
if (commonInfo->MI != UINT_MAX)
commonInfo->MI &= 0x7F;
}
static BOOL l_EnumKey_Instances(KUSB_ENUM_REGKEY_PARAMS* RegEnumParams)
{
LONG status;
PKLST_DEVINFO_EL newDevItem = NULL;
HDEVINFO hDevInfo = NULL;
DWORD length;
DWORD iDeviceInterface = (DWORD) - 1;
if (!RegEnumParams->Exclusive.DevInterfaceGuid)
{
// Apply DeviceInterfaceGUID filter:
mLst_ApplyPatternMatch(RegEnumParams->PatternMatch, DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceInterfaceGUID, goto NextInstance);
hDevInfo = SetupDiGetClassDevsA(&RegEnumParams->DevInterfaceGuid, RegEnumParams->TempItem->DeviceID, NULL, RegEnumParams->DigcFlags | DIGCF_DEVICEINTERFACE);
if (!IsHandleValid(hDevInfo))
{
USBDBGN("SetupDiGetClassDevsA Failed. ErrorCode:%08Xh", GetLastError());
return TRUE;
}
}
else
{
hDevInfo = RegEnumParams->DevInfoSet;
}
do
{
if (hDevInfo != RegEnumParams->DevInfoSet)
{
RegEnumParams->DevInterfaceData.cbSize = sizeof(RegEnumParams->DevInterfaceData);
if (!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &RegEnumParams->DevInterfaceGuid, ++iDeviceInterface, &RegEnumParams->DevInterfaceData))
goto Done;
}
else
{
iDeviceInterface++;
if (iDeviceInterface > 0) break;
}
if (!RegEnumParams->Exclusive.DevInterfaceGuid)
{
// Update 'Connected' from DevInterfaceData.Flags
mLst_HandleConnectScenario(RegEnumParams, goto NextInstance);
}
// Disconnected filter devices are never listed regardless of the KLST_FLAG_INCLUDE_DISCONNECT flag.
if (RegEnumParams->TempItem->Connected == FALSE && RegEnumParams->TempItem->DriverID == KUSB_DRVID_LIBUSB0_FILTER)
{
USBDBGN("Skipping disconnected filter device: %s (%s)",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID);
goto NextInstance;
}
if (hDevInfo != RegEnumParams->DevInfoSet)
{
mLst_Get_InterfaceDetail(RegEnumParams, hDevInfo, NULL, length, goto NextInstance);
}
// Since the 'LUsb0FilterIndex' is assigned when the device is connected, there is no point in getting it until it is.
// IE: It is invalid even if it exists.
RegEnumParams->TempItem->LUsb0FilterIndex = -1;
if ((RegEnumParams->TempItem->Connected) && (RegEnumParams->TempItem->DriverID == KUSB_DRVID_LIBUSB0 || RegEnumParams->TempItem->DriverID == KUSB_DRVID_LIBUSB0_FILTER))
{
HKEY hKeyDevInterface;
// 'LUsb0FilterIndex' is stored in the device interface key
hKeyDevInterface = SetupDiOpenDeviceInterfaceRegKey(hDevInfo, &RegEnumParams->DevInterfaceData, 0, KEY_QUERY_VALUE);
if (!IsHandleValid(hKeyDevInterface))
{
USBERRN("SetupDiOpenDeviceInterfaceRegKey Failed. ErrorCode:%08Xh", GetLastError());
goto NextInstance;
}
length = sizeof(RegEnumParams->TempItem->LUsb0FilterIndex);
status = RegQueryValueExA(hKeyDevInterface, "LUsb0", 0, NULL, (LPBYTE)&RegEnumParams->TempItem->LUsb0FilterIndex, &length);
RegCloseKey(hKeyDevInterface);
if (status != ERROR_SUCCESS)
{
USBWRNN("Failed getting 'Lusb0' index for device: %s (%s)",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID);
}
else if (RegEnumParams->TempItem->LUsb0FilterIndex > 255 || RegEnumParams->TempItem->LUsb0FilterIndex < 0)
{
USBWRNN("Invalid 'Lusb0' index for device: %s (%s)",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID);
status = ERROR_RANGE_NOT_FOUND;
}
if (status == ERROR_SUCCESS)
{
sprintf(RegEnumParams->TempItem->DevicePath, "\\\\.\\libusb0-%04d", RegEnumParams->TempItem->LUsb0FilterIndex);
}
}
// Get SPDRP_DEVICEDESC
if (!SetupDiGetDeviceRegistryPropertyA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)RegEnumParams->TempItem->DeviceDesc, sizeof(RegEnumParams->TempItem->DeviceDesc) - 1, &length))
length = 0;
RegEnumParams->TempItem->DeviceDesc[length] = (CHAR)0;
// Get SPDRP_MFG
if (!SetupDiGetDeviceRegistryPropertyA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, SPDRP_MFG, NULL, (PBYTE)RegEnumParams->TempItem->Mfg, sizeof(RegEnumParams->TempItem->Mfg) - 1, &length))
length = 0;
RegEnumParams->TempItem->Mfg[length] = (CHAR)0;
// Get SPDRP_BUSNUMBER
if (!SetupDiGetDeviceRegistryPropertyA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, SPDRP_BUSNUMBER, NULL, (PBYTE)&RegEnumParams->TempItem->BusNumber, sizeof(RegEnumParams->TempItem->BusNumber), &length))
RegEnumParams->TempItem->BusNumber = -1;
// Get SPDRP_ADDRESS
if (!SetupDiGetDeviceRegistryPropertyA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, SPDRP_ADDRESS, NULL, (PBYTE)&RegEnumParams->TempItem->DeviceAddress, sizeof(RegEnumParams->TempItem->DeviceAddress), &length))
RegEnumParams->TempItem->DeviceAddress = -1;
RegEnumParams->ErrorCode = l_Build_AddElement(RegEnumParams, &newDevItem);
if (RegEnumParams->ErrorCode != ERROR_SUCCESS)
{
goto Error;
}
l_Build_Common_Info(newDevItem);
strcpy(newDevItem->Public.SerialNumber, newDevItem->Public.DeviceID);
_strupr(newDevItem->Public.SerialNumber);
if (strstr(newDevItem->Public.SerialNumber, "&MI_") != NULL)
{
// This is a composite device. The 'SerialNumber' will come from the parent device.
DWORD hParentInst;
if (AllK->CM_Get_Parent(&hParentInst, RegEnumParams->DevInfoData.DevInst, 0) == ERROR_SUCCESS)
{
if (AllK->CM_Get_Device_ID(hParentInst, newDevItem->Public.SerialNumber, sizeof(newDevItem->Public.SerialNumber) - 1, 0) == ERROR_SUCCESS)
{
PCHAR sep = newDevItem->Public.SerialNumber;
PCHAR sepNext;
while( (sepNext = strchr(sep, '\\')) != NULL )
sep = sepNext + 1;
strcpy(newDevItem->Public.SerialNumber, sep);
goto NextInstance;
}
}
}
strcpy(newDevItem->Public.SerialNumber, newDevItem->Public.Common.InstanceID);
NextInstance:
;
}
while(TRUE);
Done:
if (hDevInfo && hDevInfo != RegEnumParams->DevInfoSet)
SetupDiDestroyDeviceInfoList(hDevInfo);
return TRUE;
Error:
if (hDevInfo && hDevInfo != RegEnumParams->DevInfoSet)
SetupDiDestroyDeviceInfoList(hDevInfo);
return FALSE;
}
static BOOL l_EnumKey_Guids(KUSB_ENUM_REGKEY_PARAMS* RegEnumParams)
{
DWORD devInfoDataIndex = (DWORD) - 1;
DWORD length;
DWORD status;
CHAR devInterfaceGuidArray[1024];
BOOL success;
LPSTR setupEnumerator = "USB";
GUID* setupClassGuid = NULL;
DWORD setupAddFlags = DIGCF_ALLCLASSES;
if (RegEnumParams->PatternMatch)
{
// If a specific DeviceInterfaceGUID or DeviceID (or both) is set in the pattern match then we can
// use these value(s) to dramatically improve performance.
if (RegEnumParams->PatternMatch->DeviceInterfaceGUID[0] &&
strlen(RegEnumParams->PatternMatch->DeviceInterfaceGUID) == GUID_STRING_LENGTH &&
String_To_Guid(&RegEnumParams->DevInterfaceGuid, RegEnumParams->PatternMatch->DeviceInterfaceGUID))
{
// PatternMatch.DeviceInterfaceGUID specifies an *exact* DeviceInterfaceGUID.
RegEnumParams->Exclusive.DevInterfaceGuid = TRUE;
setupClassGuid = &RegEnumParams->DevInterfaceGuid;
setupEnumerator = NULL;
setupAddFlags = DIGCF_DEVICEINTERFACE;
strcpy(RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->PatternMatch->DeviceInterfaceGUID);
/*
This is the 2nd best case performance scenario. The result is:
Number of Calls | Function
-----------------|----------------------------------------------------
1 | SetupDiGetClassDevs()
1-Per-Instance | SetupDiEnumDeviceInterfaces()
4-Per-Interface | SetupDiGetDeviceRegistryProperty()
1-Per-Interface | SetupDiGetDeviceInterfaceDetail()
1-Per-Interface | [libusb0 only] SetupDiOpenDeviceInterfaceRegKey()
1-Per-Interface | [libusb0 only] RegQueryValueEx()
*/
}
if (!RegEnumParams->Exclusive.DevInterfaceGuid &&
RegEnumParams->PatternMatch->ClassGUID[0] &&
strlen(RegEnumParams->PatternMatch->ClassGUID) == GUID_STRING_LENGTH &&
String_To_Guid(&RegEnumParams->ClassGuid, RegEnumParams->PatternMatch->ClassGUID))
{
// PatternMatch.ClassGUID specifies an *exact* ClassGUID.
RegEnumParams->Exclusive.ClassGuid = TRUE;
setupAddFlags = 0;
setupClassGuid = &RegEnumParams->ClassGuid;
strcpy(RegEnumParams->TempItem->ClassGUID, RegEnumParams->PatternMatch->ClassGUID);
}
}
RegEnumParams->DevInfoSet = SetupDiGetClassDevsA(setupClassGuid, setupEnumerator, NULL, RegEnumParams->DigcFlags | setupAddFlags);
if (!IsHandleValid(RegEnumParams->DevInfoSet))
{
USBDBGN("SetupDiGetClassDevs Failed. ErrorCode:%08Xh", GetLastError());
return TRUE;
}
RegEnumParams->DevInfoData.cbSize = sizeof(RegEnumParams->DevInfoData);
RegEnumParams->DevInterfaceData.cbSize = sizeof(RegEnumParams->DevInterfaceData);
do
{
if (RegEnumParams->Exclusive.DevInterfaceGuid)
{
// If the DeviceInterfaceGUID is exclusive, go directly to SetupDiEnumDeviceInterfaces.
success = SetupDiEnumDeviceInterfaces(RegEnumParams->DevInfoSet, NULL, &RegEnumParams->DevInterfaceGuid, ++devInfoDataIndex, &RegEnumParams->DevInterfaceData);
if (!success) break;
// Get the DevicePath/SymbolicLink.
// SetupDiGetDeviceInterfaceDetail will also return the DevInfoData (Instance Information) for the interface.
// NOTE: This is also done in l_EnumKey_Instances for listings that do *not* use an exclusive DevInterfaceGuid.
mLst_Get_InterfaceDetail(RegEnumParams, RegEnumParams->DevInfoSet, (&RegEnumParams->DevInfoData), length, goto NextInstance);
// Update 'Connected' from DevInterfaceData.Flags
mLst_HandleConnectScenario(RegEnumParams, goto NextInstance);
}
else
{
success = SetupDiEnumDeviceInfo(RegEnumParams->DevInfoSet, ++devInfoDataIndex, &RegEnumParams->DevInfoData);
if (!success) break;
}
if (!RegEnumParams->Exclusive.ClassGuid)
{
// Get ClassGUID
if (!Guid_To_String(&RegEnumParams->DevInfoData.ClassGuid, RegEnumParams->TempItem->ClassGUID))
goto NextInstance;
// Apply ClassGUID filter:
mLst_ApplyPatternMatch(RegEnumParams->PatternMatch, ClassGUID, RegEnumParams->TempItem->ClassGUID, goto NextInstance);
}
// Get the Device Instance ID
if (!SetupDiGetDeviceInstanceIdA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, RegEnumParams->TempItem->DeviceID, sizeof(RegEnumParams->TempItem->DeviceID), NULL))
{
USBERRN("SetupDiGetDeviceInstanceId failed. ErrorCode:%08Xh", GetLastError());
goto NextInstance;
}
// We are only interested in *usb device* instances; not root hubs (or anything else)
if (_strnicmp(RegEnumParams->TempItem->DeviceID, "USB\\VID_", 8) != 0)
{
USBDBGN("Skipping %s..", RegEnumParams->TempItem->DeviceID);
goto NextInstance;
}
// Apply PatternMatch->DeviceID
mLst_ApplyPatternMatch(RegEnumParams->PatternMatch, DeviceID, RegEnumParams->TempItem->DeviceID, goto NextInstance);
// Get SPDRP_SERVICE
if (!SetupDiGetDeviceRegistryPropertyA(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, SPDRP_SERVICE, NULL, (PBYTE)RegEnumParams->TempItem->Service, sizeof(RegEnumParams->TempItem->Service) - 1, NULL))
{
USBDBGN("SetupDiGetDeviceRegistryProperty SPDRP_SERVICE Failed. ErrorCode:%08Xh", GetLastError());
goto NextInstance;
}
RegEnumParams->TempItem->DriverID = -1;
mLst_Assign_DrvId_From_Service(RegEnumParams->TempItem->DriverID, RegEnumParams->TempItem->Service);
if (RegEnumParams->Exclusive.DevInterfaceGuid)
{
if (!l_EnumKey_Instances(RegEnumParams))
{
goto Error;
}
}
else if (RegEnumParams->TempItem->DriverID == -1)
{
// We only care about the libusb0 filter GUID for this device instance because
// the function driver is unknown. If the filter is installed, is has enabled
// the Libusb0FilterGuid.
memcpy(&RegEnumParams->DevInterfaceGuid, &Libusb0FilterGuid, sizeof(GUID));
strcpy(RegEnumParams->TempItem->DeviceInterfaceGUID, Libusb0FilterGuidA);
RegEnumParams->TempItem->DriverID = KUSB_DRVID_LIBUSB0_FILTER;
if (!l_EnumKey_Instances(RegEnumParams))
{
goto Error;
}
}
else
{
// The servicename matched a supported driver. EG: (libusbK, WinUSB, or libusb0)
// We care about the 'DeviceIntefaceGUIDs' multi-sz registry key. These drivers use
// the same key to enable the interface guids when the device is connected.
INT devInterfaceGuidArrayPos;
HKEY hkeyDevInfo = SetupDiOpenDevRegKey(RegEnumParams->DevInfoSet, &RegEnumParams->DevInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
if (!IsHandleValid(hkeyDevInfo))
{
USBERRN("SetupDiOpenDevRegKey Failed. ErrorCode:%08Xh", GetLastError());
goto NextInstance;
}
length = sizeof(devInterfaceGuidArray);
status = RegQueryValueExA(hkeyDevInfo, "DeviceInterfaceGUIDs", NULL, NULL, (LPBYTE)devInterfaceGuidArray, &length);
if (status != ERROR_SUCCESS)
{
status = RegQueryValueExA(hkeyDevInfo, "DeviceInterfaceGUID", NULL, NULL, (LPBYTE)devInterfaceGuidArray, &length);
if (status != ERROR_SUCCESS)
{
// if there isn't a DeviceInterfaceGUIDs/DeviceInterfaceGUID property then we must use the default for libusbK or libusb0
if (RegEnumParams->TempItem->DriverID == KUSB_DRVID_LIBUSB0)
{
memcpy(devInterfaceGuidArray, Libusb0DeviceGuidA, strlen(Libusb0DeviceGuidA));
length = (DWORD)strlen(Libusb0DeviceGuidA);
status = ERROR_SUCCESS;
}
else if (RegEnumParams->TempItem->DriverID == KUSB_DRVID_LIBUSBK)
{
memcpy(devInterfaceGuidArray, LibusbKDeviceGuidA, strlen(LibusbKDeviceGuidA));
length = (DWORD)strlen(LibusbKDeviceGuidA);
status = ERROR_SUCCESS;
}
}
if (status != ERROR_SUCCESS)
{
USBERRN("RegQueryValueExA Failed. ErrorCode:%08Xh", GetLastError());
RegCloseKey(hkeyDevInfo);
goto NextInstance;
}
else
{
devInterfaceGuidArray[length]=0;
devInterfaceGuidArray[length+1]=0;
devInterfaceGuidArray[length+2]=0;
}
}
RegCloseKey(hkeyDevInfo);
devInterfaceGuidArrayPos = 0;
while(devInterfaceGuidArray[devInterfaceGuidArrayPos])
{
length = (DWORD)strlen(&devInterfaceGuidArray[devInterfaceGuidArrayPos]);
strcpy(RegEnumParams->TempItem->DeviceInterfaceGUID, &devInterfaceGuidArray[devInterfaceGuidArrayPos]);
if (String_To_Guid(&RegEnumParams->DevInterfaceGuid, RegEnumParams->TempItem->DeviceInterfaceGUID))
{
if (!l_EnumKey_Instances(RegEnumParams))
{
goto Error;
}
}
devInterfaceGuidArrayPos += length + 1;
}
}
NextInstance:
;
}
while(success);
Error:
SetupDiDestroyDeviceInfoList(RegEnumParams->DevInfoSet);
return RegEnumParams->ErrorCode == ERROR_SUCCESS;
}
#if 0
static BOOL l_EnumKey_Guids_Direct(KUSB_ENUM_REGKEY_PARAMS* RegEnumParams)
{
DWORD length;
DWORD status;
HKEY hKeyDeviceClassRoot;
DWORD iDeviceInterfaceGUID = (DWORD) - 1;
status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, KEY_DEVICECLASSES, 0, KEY_READ, &hKeyDeviceClassRoot);
if (status != ERROR_SUCCESS)
{
USBERRN("Opening 'DeviceClasses' registry key failed. ErrorCode=%08Xh", status);
return FALSE;
}
while(RegEnumKeyA(hKeyDeviceClassRoot, ++iDeviceInterfaceGUID, RegEnumParams->TempItem->DeviceInterfaceGUID, sizeof(RegEnumParams->TempItem->DeviceInterfaceGUID)) == ERROR_SUCCESS)
{
HKEY hKeyDeviceInterfaceGUID;
if (!(RegEnumParams->Flags & KLST_FLAG_INCLUDE_RAWGUID))
{
if (_stricmp(RegEnumParams->TempItem->DeviceInterfaceGUID, RawDeviceGuidA) == 0) continue;
}
status = RegOpenKeyA(hKeyDeviceClassRoot, RegEnumParams->TempItem->DeviceInterfaceGUID, &hKeyDeviceInterfaceGUID);
if (status == ERROR_SUCCESS)
{
DWORD iSymbolicLink = (DWORD) - 1;
while(RegEnumKeyA(hKeyDeviceInterfaceGUID, ++iSymbolicLink, RegEnumParams->TempItem->SymbolicLink, sizeof(RegEnumParams->TempItem->SymbolicLink)) == ERROR_SUCCESS)
{
HKEY hKeySymbolicLink;
if (_strnicmp(RegEnumParams->TempItem->SymbolicLink, "##?#USB#VID_", 12) != 0) break;
status = RegOpenKeyA(hKeyDeviceInterfaceGUID, RegEnumParams->TempItem->SymbolicLink, &hKeySymbolicLink);
if (status == ERROR_SUCCESS)
{
length = sizeof(RegEnumParams->TempItem->DeviceID) - 1;
status = RegQueryValueExA(hKeySymbolicLink, "DeviceInstance", 0, NULL, (LPBYTE)RegEnumParams->TempItem->DeviceID, &length);
if (status == ERROR_SUCCESS)
{
HKEY hKeyDeviceParameters;
HDEVINFO hDevInfo;
RegEnumParams->TempItem->DeviceID[length] = '\0';
status = RegOpenKeyA(hKeySymbolicLink, "#\\Device Parameters", &hKeyDeviceParameters);
if (status == ERROR_SUCCESS)
{
length = sizeof(RegEnumParams->TempItem->LUsb0FilterIndex);
status = RegQueryValueExA(hKeyDeviceParameters, "LUsb0", 0, NULL, (LPBYTE)RegEnumParams->TempItem->LUsb0FilterIndex, &length);
RegCloseKey(hKeyDeviceParameters);
if (status != ERROR_SUCCESS || RegEnumParams->TempItem->LUsb0FilterIndex > 255 || RegEnumParams->TempItem->LUsb0FilterIndex < 0)
RegEnumParams->TempItem->LUsb0FilterIndex = -1;
}
RegEnumParams->TempItem->DriverID = -1;
mLst_Assign_DrvId_From_InterfaceGuid(RegEnumParams->TempItem->DriverID, RegEnumParams->TempItem->DeviceInterfaceGUID);
RegEnumParams->TempItem->SymbolicLink[0] = '\\';
RegEnumParams->TempItem->SymbolicLink[1] = '\\';
RegEnumParams->TempItem->SymbolicLink[2] = '?';
RegEnumParams->TempItem->SymbolicLink[3] = '\\';
strcpy(RegEnumParams->TempItem->DevicePath, RegEnumParams->TempItem->SymbolicLink);
RegEnumParams->TempItem->Connected = FALSE;
l_String_To_Guid(&RegEnumParams->DevInterfaceGuid, RegEnumParams->TempItem->DeviceInterfaceGUID);
hDevInfo = SetupDiGetClassDevsA(&RegEnumParams->DevInterfaceGuid, RegEnumParams->TempItem->DeviceID, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (IsHandleValid(hDevInfo))
{
RegEnumParams->DevInterfaceData.cbSize = sizeof(RegEnumParams->DevInterfaceData);
if (SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &RegEnumParams->DevInterfaceGuid, 0, &RegEnumParams->DevInterfaceData))
{
if (RegEnumParams->DevInterfaceData.Flags & SPINT_ACTIVE)
{
RegEnumParams->TempItem->Connected = TRUE;
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
USBDBGN("%s (%s) Connected:%u",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID, RegEnumParams->TempItem->Connected);
#if 0
if (!RegEnumParams->TempItem->Connected)
{
DEVINST hDevInst;
if (CM_Locate_DevNodeA(&hDevInst, RegEnumParams->TempItem->DeviceID, CM_LOCATE_DEVNODE_NORMAL) == CR_SUCCESS)
{
// DWORD dnStatus;
// DWORD dnProblem;
RegEnumParams->TempItem->Connected = TRUE;
USBMSGN("%s (%s) Connected:TUE\n",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID);
if (CM_Get_DevNode_Status(&dnStatus, &dnProblem, hDevInst, 0) == CR_SUCCESS)
{
printf("%s (%s) Status:%08Xh Problem:%08Xh\n",
RegEnumParams->TempItem->DeviceInterfaceGUID, RegEnumParams->TempItem->DeviceID, dnStatus, dnProblem);
}
}
}
#endif
}
RegCloseKey(hKeySymbolicLink);
}
}
RegCloseKey(hKeyDeviceInterfaceGUID);
}
}
RegCloseKey(hKeyDeviceClassRoot);
//return RegEnumParams->ErrorCode == ERROR_SUCCESS;
return FALSE;
}
#endif
KUSB_EXP BOOL KUSB_API LstK_Count(
_in KLST_HANDLE DeviceList,
_ref PUINT Count)
{
PKLST_HANDLE_INTERNAL handle;
PKLST_DEVINFO_EL nextItem = NULL;