-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcer_USB_Library.cs
1011 lines (837 loc) · 35.5 KB
/
Acer_USB_Library.cs
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
// Acer_Class.Acer_USB_Library
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Text;
public class Acer_USB_Library
{
private enum USB_HUB_NODE
{
UsbHub,
UsbMIParent
}
private enum USB_CONNECTION_STATUS
{
NoDeviceConnected,
DeviceConnected,
DeviceFailedEnumeration,
DeviceGeneralFailure,
DeviceCausedOvercurrent,
DeviceNotEnoughPower,
DeviceNotEnoughBandwidth,
DeviceHubNestedTooDeeply,
DeviceInLegacyHub
}
private enum USB_DEVICE_SPEED : byte
{
UsbLowSpeed,
UsbFullSpeed,
UsbHighSpeed,
UsbSuperSpeed
}
private struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public IntPtr Reserved;
}
private struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public string DevicePath;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct USB_HCD_DRIVERKEY_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public string DriverKeyName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct USB_ROOT_HUB_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public string RootHubName;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct USB_HUB_DESCRIPTOR
{
public byte bDescriptorLength;
public byte bDescriptorType;
public byte bNumberOfPorts;
public short wHubCharacteristics;
public byte bPowerOnToPowerGood;
public byte bHubControlCurrent;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] bRemoveAndPowerMask;
}
private struct USB_HUB_INFORMATION
{
public USB_HUB_DESCRIPTOR HubDescriptor;
public byte HubIsBusPowered;
}
private struct USB_NODE_INFORMATION
{
public int NodeType;
public USB_HUB_INFORMATION HubInformation;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct USB_NODE_CONNECTION_INFORMATION_EX
{
public int ConnectionIndex;
public USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public byte CurrentConfigurationValue;
public byte Speed;
public byte DeviceIsHub;
public short DeviceAddress;
public int NumberOfOpenPipes;
public int ConnectionStatus;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct USB_DEVICE_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
public short bcdUSB;
public byte bDeviceClass;
public byte bDeviceSubClass;
public byte bDeviceProtocol;
public byte bMaxPacketSize0;
public ushort idVendor;
public ushort idProduct;
public short bcdDevice;
public byte iManufacturer;
public byte iProduct;
public byte iSerialNumber;
public byte bNumConfigurations;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct USB_STRING_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string bString;
}
private struct USB_SETUP_PACKET
{
public byte bmRequest;
public byte bRequest;
public short wValue;
public short wIndex;
public short wLength;
}
private struct USB_DESCRIPTOR_REQUEST
{
public int ConnectionIndex;
public USB_SETUP_PACKET SetupPacket;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct USB_NODE_CONNECTION_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public string NodeName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct USB_NODE_CONNECTION_DRIVERKEY_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2048)]
public string DriverKeyName;
}
public class USBController
{
internal int ControllerIndex;
internal string ControllerDriverKeyName;
internal string ControllerDevicePath;
internal string ControllerDeviceDesc;
public int Index
{
get
{
return this.ControllerIndex;
}
}
public string DevicePath
{
get
{
return this.ControllerDevicePath;
}
}
public string DriverKeyName
{
get
{
return this.ControllerDriverKeyName;
}
}
public string Name
{
get
{
return this.ControllerDeviceDesc;
}
}
public USBController()
{
this.ControllerIndex = 0;
this.ControllerDevicePath = "";
this.ControllerDeviceDesc = "";
this.ControllerDriverKeyName = "";
}
public USBHub GetRootHub()
{
USBHub uSBHub = new USBHub();
uSBHub.HubIsRootHub = true;
uSBHub.HubDeviceDesc = "Root Hub";
IntPtr intPtr = Acer_USB_Library.CreateFile(this.ControllerDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr.ToInt32() != -1)
{
int num = Marshal.SizeOf(default(USB_ROOT_HUB_NAME));
IntPtr intPtr2 = Marshal.AllocHGlobal(num);
int num2 = default(int);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229256, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
{
USB_ROOT_HUB_NAME uSB_ROOT_HUB_NAME = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_ROOT_HUB_NAME));
uSBHub.HubDevicePath = "\\\\.\\" + uSB_ROOT_HUB_NAME.RootHubName;
}
IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr3.ToInt32() != -1)
{
USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION);
uSB_NODE_INFORMATION.NodeType = 0;
num = Marshal.SizeOf(uSB_NODE_INFORMATION);
IntPtr intPtr4 = Marshal.AllocHGlobal(num);
Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true);
if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero))
{
uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION));
uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered);
uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(intPtr4);
Acer_USB_Library.CloseHandle(intPtr3);
}
Marshal.FreeHGlobal(intPtr2);
Acer_USB_Library.CloseHandle(intPtr);
}
return uSBHub;
}
}
public class USBHub
{
internal int HubPortCount;
internal string HubDriverKey;
internal string HubDevicePath;
internal string HubDeviceDesc;
internal string HubManufacturer;
internal string HubProduct;
internal string HubSerialNumber;
internal string HubInstanceID;
internal bool HubIsBusPowered;
internal bool HubIsRootHub;
public int PortCount
{
get
{
return this.HubPortCount;
}
}
public string DevicePath
{
get
{
return this.HubDevicePath;
}
}
public string DriverKey
{
get
{
return this.HubDriverKey;
}
}
public string Name
{
get
{
return this.HubDeviceDesc;
}
}
public string InstanceID
{
get
{
return this.HubInstanceID;
}
}
public bool IsBusPowered
{
get
{
return this.HubIsBusPowered;
}
}
public bool IsRootHub
{
get
{
return this.HubIsRootHub;
}
}
public string Manufacturer
{
get
{
return this.HubManufacturer;
}
}
public string Product
{
get
{
return this.HubProduct;
}
}
public string SerialNumber
{
get
{
return this.HubSerialNumber;
}
}
public USBHub()
{
this.HubPortCount = 0;
this.HubDevicePath = "";
this.HubDeviceDesc = "";
this.HubDriverKey = "";
this.HubIsBusPowered = false;
this.HubIsRootHub = false;
this.HubManufacturer = "";
this.HubProduct = "";
this.HubSerialNumber = "";
this.HubInstanceID = "";
}
public ReadOnlyCollection<USBPort> GetPorts()
{
List<USBPort> list = new List<USBPort>();
IntPtr intPtr = Acer_USB_Library.CreateFile(this.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr.ToInt32() != -1)
{
int num = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
IntPtr intPtr2 = Marshal.AllocHGlobal(num);
for (int i = 1; i <= this.HubPortCount; i++)
{
USB_NODE_CONNECTION_INFORMATION_EX uSB_NODE_CONNECTION_INFORMATION_EX = default(USB_NODE_CONNECTION_INFORMATION_EX);
uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex = i;
Marshal.StructureToPtr(uSB_NODE_CONNECTION_INFORMATION_EX, intPtr2, true);
int num2 = default(int);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229320, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
{
uSB_NODE_CONNECTION_INFORMATION_EX = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_INFORMATION_EX));
USBPort uSBPort = new USBPort();
uSBPort.PortPortNumber = i;
uSBPort.PortHubDevicePath = this.HubDevicePath;
USB_CONNECTION_STATUS connectionStatus = (USB_CONNECTION_STATUS)uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus;
uSBPort.PortStatus = connectionStatus.ToString();
USB_DEVICE_SPEED speed = (USB_DEVICE_SPEED)uSB_NODE_CONNECTION_INFORMATION_EX.Speed;
uSBPort.PortSpeed = speed.ToString();
uSBPort.PortIsDeviceConnected = (uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus == 1);
uSBPort.PortIsHub = Convert.ToBoolean(uSB_NODE_CONNECTION_INFORMATION_EX.DeviceIsHub);
uSBPort.PortDeviceDescriptor = uSB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor;
list.Add(uSBPort);
}
}
Marshal.FreeHGlobal(intPtr2);
Acer_USB_Library.CloseHandle(intPtr);
}
return new ReadOnlyCollection<USBPort>(list);
}
}
public class USBPort
{
internal int PortPortNumber;
internal string PortStatus;
internal string PortHubDevicePath;
internal string PortSpeed;
internal bool PortIsHub;
internal bool PortIsDeviceConnected;
internal USB_DEVICE_DESCRIPTOR PortDeviceDescriptor;
public int PortNumber
{
get
{
return this.PortPortNumber;
}
}
public string HubDevicePath
{
get
{
return this.PortHubDevicePath;
}
}
public string Status
{
get
{
return this.PortStatus;
}
}
public string Speed
{
get
{
return this.PortSpeed;
}
}
public bool IsHub
{
get
{
return this.PortIsHub;
}
}
public bool IsDeviceConnected
{
get
{
return this.PortIsDeviceConnected;
}
}
public USBPort()
{
this.PortPortNumber = 0;
this.PortStatus = "";
this.PortHubDevicePath = "";
this.PortSpeed = "";
this.PortIsHub = false;
this.PortIsDeviceConnected = false;
}
public USBDevice GetDevice()
{
if (!this.PortIsDeviceConnected)
{
return null;
}
USBDevice uSBDevice = new USBDevice();
uSBDevice.DevicePortNumber = this.PortPortNumber;
uSBDevice.DeviceHubDevicePath = this.PortHubDevicePath;
uSBDevice.DeviceDescriptor = this.PortDeviceDescriptor;
uSBDevice.DeviceVID = this.PortDeviceDescriptor.idVendor;
uSBDevice.DevicePID = this.PortDeviceDescriptor.idProduct;
IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr.ToInt32() != -1)
{
int num = 2048;
string s = new string('\0', 2048 / Marshal.SystemDefaultCharSize);
int num2 = default(int);
if (this.PortDeviceDescriptor.iManufacturer > 0)
{
USB_DESCRIPTOR_REQUEST structure = default(USB_DESCRIPTOR_REQUEST);
structure.ConnectionIndex = this.PortPortNumber;
structure.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iManufacturer);
structure.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure));
structure.SetupPacket.wIndex = 1033;
IntPtr intPtr2 = Marshal.StringToHGlobalAuto(s);
Marshal.StructureToPtr(structure, intPtr2, true);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
{
USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr2.ToInt32() + Marshal.SizeOf(structure)), typeof(USB_STRING_DESCRIPTOR));
uSBDevice.DeviceManufacturer = uSB_STRING_DESCRIPTOR.bString;
}
Marshal.FreeHGlobal(intPtr2);
}
if (this.PortDeviceDescriptor.iProduct > 0)
{
USB_DESCRIPTOR_REQUEST structure2 = default(USB_DESCRIPTOR_REQUEST);
structure2.ConnectionIndex = this.PortPortNumber;
structure2.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iProduct);
structure2.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure2));
structure2.SetupPacket.wIndex = 1033;
IntPtr intPtr3 = Marshal.StringToHGlobalAuto(s);
Marshal.StructureToPtr(structure2, intPtr3, true);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr3, num, intPtr3, num, out num2, IntPtr.Zero))
{
USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR2 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr3.ToInt32() + Marshal.SizeOf(structure2)), typeof(USB_STRING_DESCRIPTOR));
uSBDevice.DeviceProduct = uSB_STRING_DESCRIPTOR2.bString;
}
Marshal.FreeHGlobal(intPtr3);
}
if (this.PortDeviceDescriptor.iSerialNumber > 0)
{
USB_DESCRIPTOR_REQUEST structure3 = default(USB_DESCRIPTOR_REQUEST);
structure3.ConnectionIndex = this.PortPortNumber;
structure3.SetupPacket.wValue = (short)(768 + this.PortDeviceDescriptor.iSerialNumber);
structure3.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure3));
structure3.SetupPacket.wIndex = 1033;
IntPtr intPtr4 = Marshal.StringToHGlobalAuto(s);
Marshal.StructureToPtr(structure3, intPtr4, true);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero))
{
USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR3 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr4.ToInt32() + Marshal.SizeOf(structure3)), typeof(USB_STRING_DESCRIPTOR));
uSBDevice.DeviceSerialNumber = uSB_STRING_DESCRIPTOR3.bString;
}
Marshal.FreeHGlobal(intPtr4);
}
USB_NODE_CONNECTION_DRIVERKEY_NAME uSB_NODE_CONNECTION_DRIVERKEY_NAME = default(USB_NODE_CONNECTION_DRIVERKEY_NAME);
uSB_NODE_CONNECTION_DRIVERKEY_NAME.ConnectionIndex = this.PortPortNumber;
num = Marshal.SizeOf(uSB_NODE_CONNECTION_DRIVERKEY_NAME);
IntPtr intPtr5 = Marshal.AllocHGlobal(num);
Marshal.StructureToPtr(uSB_NODE_CONNECTION_DRIVERKEY_NAME, intPtr5, true);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229280, intPtr5, num, intPtr5, num, out num2, IntPtr.Zero))
{
uSB_NODE_CONNECTION_DRIVERKEY_NAME = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(intPtr5, typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME));
uSBDevice.DeviceDriverKey = uSB_NODE_CONNECTION_DRIVERKEY_NAME.DriverKeyName;
uSBDevice.DeviceName = Acer_USB_Library.GetDescriptionByKeyName(uSBDevice.DeviceDriverKey);
uSBDevice.DeviceInstanceID = Acer_USB_Library.GetInstanceIDByKeyName(uSBDevice.DeviceDriverKey);
}
Marshal.FreeHGlobal(intPtr5);
Acer_USB_Library.CloseHandle(intPtr);
}
return uSBDevice;
}
public USBHub GetHub()
{
if (!this.PortIsHub)
{
return null;
}
USBHub uSBHub = new USBHub();
uSBHub.HubIsRootHub = false;
uSBHub.HubDeviceDesc = "External Hub";
IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr.ToInt32() != -1)
{
USB_NODE_CONNECTION_NAME uSB_NODE_CONNECTION_NAME = default(USB_NODE_CONNECTION_NAME);
uSB_NODE_CONNECTION_NAME.ConnectionIndex = this.PortPortNumber;
int num = Marshal.SizeOf(uSB_NODE_CONNECTION_NAME);
IntPtr intPtr2 = Marshal.AllocHGlobal(num);
Marshal.StructureToPtr(uSB_NODE_CONNECTION_NAME, intPtr2, true);
int num2 = default(int);
if (Acer_USB_Library.DeviceIoControl(intPtr, 2229268, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
{
uSB_NODE_CONNECTION_NAME = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_NAME));
uSBHub.HubDevicePath = "\\\\.\\" + uSB_NODE_CONNECTION_NAME.NodeName;
}
IntPtr intPtr3 = Acer_USB_Library.CreateFile(uSBHub.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (intPtr3.ToInt32() != -1)
{
USB_NODE_INFORMATION uSB_NODE_INFORMATION = default(USB_NODE_INFORMATION);
uSB_NODE_INFORMATION.NodeType = 0;
num = Marshal.SizeOf(uSB_NODE_INFORMATION);
IntPtr intPtr4 = Marshal.AllocHGlobal(num);
Marshal.StructureToPtr(uSB_NODE_INFORMATION, intPtr4, true);
if (Acer_USB_Library.DeviceIoControl(intPtr3, 2229256, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero))
{
uSB_NODE_INFORMATION = (USB_NODE_INFORMATION)Marshal.PtrToStructure(intPtr4, typeof(USB_NODE_INFORMATION));
uSBHub.HubIsBusPowered = Convert.ToBoolean(uSB_NODE_INFORMATION.HubInformation.HubIsBusPowered);
uSBHub.HubPortCount = uSB_NODE_INFORMATION.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(intPtr4);
Acer_USB_Library.CloseHandle(intPtr3);
}
USBDevice device = this.GetDevice();
uSBHub.HubInstanceID = device.DeviceInstanceID;
uSBHub.HubManufacturer = device.Manufacturer;
uSBHub.HubProduct = device.Product;
uSBHub.HubSerialNumber = device.SerialNumber;
uSBHub.HubDriverKey = device.DriverKey;
Marshal.FreeHGlobal(intPtr2);
Acer_USB_Library.CloseHandle(intPtr);
}
return uSBHub;
}
}
public class USBDevice
{
internal int DevicePortNumber;
internal ushort DeviceVID;
internal ushort DevicePID;
internal string DeviceDriverKey;
internal string DeviceHubDevicePath;
internal string DeviceInstanceID;
internal string DeviceName;
internal string DeviceManufacturer;
internal string DeviceProduct;
internal string DeviceSerialNumber;
internal USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public ushort VID
{
get
{
return this.DeviceVID;
}
}
public ushort PID
{
get
{
return this.DevicePID;
}
}
public int PortNumber
{
get
{
return this.DevicePortNumber;
}
}
public string HubDevicePath
{
get
{
return this.DeviceHubDevicePath;
}
}
public string DriverKey
{
get
{
return this.DeviceDriverKey;
}
}
public string InstanceID
{
get
{
return this.DeviceInstanceID;
}
}
public string Name
{
get
{
return this.DeviceName;
}
}
public string Manufacturer
{
get
{
return this.DeviceManufacturer;
}
}
public string Product
{
get
{
return this.DeviceProduct;
}
}
public string SerialNumber
{
get
{
return this.DeviceSerialNumber;
}
}
public USBDevice()
{
this.DevicePortNumber = 0;
this.DeviceHubDevicePath = "";
this.DeviceDriverKey = "";
this.DeviceManufacturer = "";
this.DeviceProduct = "Unknown USB Device";
this.DeviceSerialNumber = "";
this.DeviceName = "";
this.DeviceInstanceID = "";
this.DeviceVID = 0;
this.DevicePID = 0;
}
}
private const int GENERIC_WRITE = 1073741824;
private const int FILE_SHARE_READ = 1;
private const int FILE_SHARE_WRITE = 2;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
private const int IOCTL_GET_HCD_DRIVERKEY_NAME = 2229284;
private const int IOCTL_USB_GET_ROOT_HUB_NAME = 2229256;
private const int IOCTL_USB_GET_NODE_INFORMATION = 2229256;
private const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 2229320;
private const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 2229264;
private const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 2229268;
private const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 2229280;
private const int USB_DEVICE_DESCRIPTOR_TYPE = 1;
private const int USB_STRING_DESCRIPTOR_TYPE = 3;
private const int BUFFER_SIZE = 2048;
private const int MAXIMUM_USB_STRING_LENGTH = 255;
private const string GUID_DEVINTERFACE_HUBCONTROLLER = "3abf6f2d-71c4-462a-8a92-1e6861e6af27";
private const string REGSTR_KEY_USB = "USB";
private const int DIGCF_PRESENT = 2;
private const int DIGCF_ALLCLASSES = 4;
private const int DIGCF_DEVICEINTERFACE = 16;
private const int SPDRP_DRIVER = 9;
private const int SPDRP_DEVICEDESC = 0;
private const int REG_SZ = 1;
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, int Enumerator, IntPtr hwndParent, int Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SetupDiGetClassDevs(int ClassGuid, string Enumerator, IntPtr hwndParent, int Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, int iProperty, ref int PropertyRegDataType, IntPtr PropertyBuffer, int PropertyBufferSize, ref int RequiredSize);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, StringBuilder DeviceInstanceId, int DeviceInstanceIdSize, out int RequiredSize);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
public static ReadOnlyCollection<USBController> GetHostControllers()
{
List<USBController> list = new List<USBController>();
Guid guid = new Guid("3abf6f2d-71c4-462a-8a92-1e6861e6af27");
IntPtr deviceInfoSet = Acer_USB_Library.SetupDiGetClassDevs(ref guid, 0, IntPtr.Zero, 18);
if (deviceInfoSet.ToInt32() != -1)
{
IntPtr intPtr = Marshal.AllocHGlobal(2048);
int num = 0;
bool flag;
do
{
USBController uSBController = new USBController();
uSBController.ControllerIndex = num;
SP_DEVICE_INTERFACE_DATA structure = default(SP_DEVICE_INTERFACE_DATA);
structure.cbSize = Marshal.SizeOf(structure);
flag = Acer_USB_Library.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref guid, num, ref structure);
if (flag)
{
SP_DEVINFO_DATA structure2 = default(SP_DEVINFO_DATA);
structure2.cbSize = Marshal.SizeOf(structure2);
SP_DEVICE_INTERFACE_DETAIL_DATA sP_DEVICE_INTERFACE_DETAIL_DATA = default(SP_DEVICE_INTERFACE_DETAIL_DATA);
sP_DEVICE_INTERFACE_DETAIL_DATA.cbSize = 4 + Marshal.SystemDefaultCharSize;
int num2 = 0;
int deviceInterfaceDetailDataSize = 2048;
if (Acer_USB_Library.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref structure, ref sP_DEVICE_INTERFACE_DETAIL_DATA, deviceInterfaceDetailDataSize, ref num2, ref structure2))
{
uSBController.ControllerDevicePath = sP_DEVICE_INTERFACE_DETAIL_DATA.DevicePath;
int num3 = 0;
int num4 = 1;
if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure2, 0, ref num4, intPtr, 2048, ref num3))
{
uSBController.ControllerDeviceDesc = Marshal.PtrToStringAuto(intPtr);
}
if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure2, 9, ref num4, intPtr, 2048, ref num3))
{
uSBController.ControllerDriverKeyName = Marshal.PtrToStringAuto(intPtr);
}
}
list.Add(uSBController);
}
num++;
}
while (flag);
Marshal.FreeHGlobal(intPtr);
Acer_USB_Library.SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
return new ReadOnlyCollection<USBController>(list);
}
private static string GetDescriptionByKeyName(string DriverKeyName)
{
string result = "";
string enumerator = "USB";
IntPtr deviceInfoSet = Acer_USB_Library.SetupDiGetClassDevs(0, enumerator, IntPtr.Zero, 6);
if (deviceInfoSet.ToInt32() != -1)
{
IntPtr intPtr = Marshal.AllocHGlobal(2048);
int num = 0;
bool flag;
do
{
SP_DEVINFO_DATA structure = default(SP_DEVINFO_DATA);
structure.cbSize = Marshal.SizeOf(structure);
flag = Acer_USB_Library.SetupDiEnumDeviceInfo(deviceInfoSet, num, ref structure);
if (flag)
{
int num2 = 0;
int num3 = 1;
string a = "";
if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure, 9, ref num3, intPtr, 2048, ref num2))
{
a = Marshal.PtrToStringAuto(intPtr);
}
if (a == DriverKeyName)
{
if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure, 0, ref num3, intPtr, 2048, ref num2))
{
result = Marshal.PtrToStringAuto(intPtr);
}
break;
}
}
num++;
}
while (flag);
Marshal.FreeHGlobal(intPtr);
Acer_USB_Library.SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
return result;
}
private static string GetInstanceIDByKeyName(string DriverKeyName)
{
string result = "";
string enumerator = "USB";
IntPtr deviceInfoSet = Acer_USB_Library.SetupDiGetClassDevs(0, enumerator, IntPtr.Zero, 6);
if (deviceInfoSet.ToInt32() != -1)
{
IntPtr intPtr = Marshal.AllocHGlobal(2048);
int num = 0;
bool flag;
do
{
SP_DEVINFO_DATA structure = default(SP_DEVINFO_DATA);
structure.cbSize = Marshal.SizeOf(structure);
flag = Acer_USB_Library.SetupDiEnumDeviceInfo(deviceInfoSet, num, ref structure);
if (flag)
{
int num2 = 0;
int num3 = 1;
string a = "";
if (Acer_USB_Library.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref structure, 9, ref num3, intPtr, 2048, ref num2))
{
a = Marshal.PtrToStringAuto(intPtr);
}
if (a == DriverKeyName)
{
int num4 = 2048;
StringBuilder stringBuilder = new StringBuilder(num4);
Acer_USB_Library.SetupDiGetDeviceInstanceId(deviceInfoSet, ref structure, stringBuilder, num4, out num2);
result = stringBuilder.ToString();
break;