-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuusbwinimplementation.inc
465 lines (444 loc) · 18.4 KB
/
uusbwinimplementation.inc
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
function TUSBController.USBRegister: Boolean;
var
dbi: DEV_BROADCAST_DEVICEINTERFACE;
Size: Integer;
r: Pointer;
begin
Result := False;
Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
ZeroMemory(@dbi, Size);
dbi.dbcc_size := Size;
dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved := 0;
dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name := 0;
r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
if Assigned(r) then Result := True;
end;
function TUSBController.GetUSBDeviceClass(VendorID, DeviceID: word
): TUSBDeviceClass;
begin
Result := TUSBDevice;
if Assigned(FOnGetDeviceClass) then
FOnGetDeviceClass(VendorID,DeviceID,Result);
end;
constructor TUSBController.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := LCLIntf.AllocateHWnd(@WndProc);
FDeviceList := TList.Create;
FBusList := TList.Create;
USBRegister;
end;
destructor TUSBController.Destroy;
var
i: Integer;
begin
for i := 0 to FBusList.Count-1 do
TUSBHostController(FBusList[i]).Free;
FDeviceList.Free;
FBusList.Free;
LCLIntf.DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
procedure TUSBController.WndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then
begin
try
WMDeviceChange(Msg);
except
Application.HandleException(Self);
end;
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
procedure TUSBController.WMDeviceChange(var Msg: TMessage);
var
devType: Integer;
Datos: PDevBroadcastHdr;
begin
if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
begin
Datos := PDevBroadcastHdr(Msg.lParam);
devType := Datos^.dbch_devicetype;
if devType = DBT_DEVTYP_DEVICEINTERFACE then
RefreshList;
end
else if Msg.wParam = 7 then
RefreshList;
end;
function UnicodeStr(uni :TUnicodeName) :string;
var
s :string;
i :word;
begin
s:='';
for i:=0 to TUnicodeNameMaxLong - 1 do
if uni[i]<>#0 then s:=s + uni[i] else break;
UnicodeStr := s;
end;
function UnicodeStrLen(len :DWord; uni :TUnicodeName) :string;
var
s :string;
i :DWord;
begin
s:='';
for i:=0 to len - 1 do
if uni[i]<>#0 then s:=s + uni[i] else break;
UnicodeStrLen := s;
end;
function USB_GetLangID(hHub :THandle; PortIndex :DWord) :DWord;
var
BytesReturned :DWord;
Packet :TDESCRIPTOR_REQUEST;
begin
fillchar(Packet, sizeof(Packet), 0);
with Packet do begin
ConnectionIndex := PortIndex;
SetupPacket.bmRequest := $80;
SetupPacket.bRequest := USB_REQUEST_GET_DESCRIPTOR;
SetupPacket.wValueHi := USB_STRING_DESCRIPTOR_TYPE;
SetupPacket.wLength := 4;
end;
if DeviceIoControl(hHub,
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
@Packet, sizeof(Packet),
@Packet, sizeof(Packet),
BytesReturned, nil)
then
USB_GetLangID := Packet.Data[2] or (Packet.Data[3] shl 8)
else
USB_GetLangID := 0;
end;
function USB_GetDescrStr(hHub :THandle; PortIndex :DWord; LangID :Word; Index :Byte) :string;
var
BytesReturned :DWord;
Packet :TDESCRIPTOR_REQUEST;
p :PUSB_STRING_DESCRIPTOR;
begin
fillchar(Packet, sizeof(Packet), 0);
with Packet do begin
ConnectionIndex := PortIndex;
SetupPacket.bmRequest := $80;
SetupPacket.bRequest := USB_REQUEST_GET_DESCRIPTOR;
SetupPacket.wValueHi := USB_STRING_DESCRIPTOR_TYPE;
SetupPacket.wValueLo := Index;
SetupPacket.wIndex := LangID;
SetupPacket.wLength := 255;
end;
if DeviceIoControl(hHub,
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
@Packet, sizeof(Packet),
@Packet, sizeof(Packet),
BytesReturned, nil)
then
USB_GetDescrStr := UnicodeStrLen(p^.bLength, p^.bString)
else
USB_GetDescrStr := 'error';
end;
procedure TUSBController.RefreshList;
var
HostNum: Integer;
DevName: String;
hHost: LongWord;
aHostController: TUSBHostController;
i: Integer;
UnicodeName : USB_HUB_NAME;
BytesReturned :DWord;
aRootHub: TUSBHub;
pc: PChar;
procedure EnumHubDevices(Hub : TUSBHub);
var
hHub: LongWord;
hHub2: LongWord;
NodeInfo :TNODE_INFORMATION;
NodeInfo2 :TNODE_INFORMATION;
ConInfo :TNODE_CONNECTION_INFORMATION;
ConnectedHub :USB_NODE_CONNECTION_NAME;
BytesReturned :DWord;
a: Integer;
b: Integer;
DeviceClass: TUSBDeviceClass;
begin
hHub := CreateFile(pchar(Hub.Path),GENERIC_WRITE,FILE_SHARE_WRITE,@SA, OPEN_EXISTING, 0, 0);
if hHub <> INVALID_HANDLE_VALUE then
begin
if DeviceIoControl(hHub,IOCTL_USB_GET_NODE_INFORMATION,@NodeInfo, sizeof(NodeInfo),@NodeInfo, sizeof(NodeInfo),BytesReturned, nil) then
begin
Hub.FBusPowered := NodeInfo.HubIsBusPowered;
while Hub.Count > NodeInfo.HubDescriptor.bNumberOfPorts do //Is this possible (nessessary) ??
begin
if Hub.Items[Hub.Count-1] <> nil then
begin
FDeviceList.Remove(Hub.Items[Hub.Count-1]);
TUSBgenericDevice(Hub.Items[Hub.Count-1]).Free;
end;
Hub.Delete(Hub.Count-1);
end;
while Hub.Count < NodeInfo.HubDescriptor.bNumberOfPorts do
Hub.Add(nil);
for a := 0 to NodeInfo.HubDescriptor.bNumberOfPorts-1 do
begin
ConInfo.ConnectionIndex := a+1;
if DeviceIoControl(hHub,IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,@ConInfo, sizeof(ConInfo),@ConInfo, sizeof(ConInfo),BytesReturned, nil) then
begin
if (ConInfo.ConnectionStatus[0] = 0) then
begin
//no device connected, do disconnect
if (Hub.Items[a] <> nil) then
begin
FDeviceList.Remove(Hub.Items[a]);
TUSBGenericDevice(Hub.Items[a]).Free;
end;
Hub.Items[a] := nil;
end
else
begin
ConnectedHub.ConnectionIndex:=a+1;
ConnectedHub.ActualLength:=sizeof(ConnectedHub);
if ConInfo.DeviceIsHub then
begin
if DeviceIoControl(hHub,IOCTL_USB_GET_NODE_CONNECTION_NAME,@ConnectedHub, sizeof(ConnectedHub),@ConnectedHub, sizeof(ConnectedHub),BytesReturned, nil) then
devname := UnicodeStrLen(ConnectedHub.ActualLength,ConnectedHub.NodeName);
if (not Assigned(Hub.Items[a]))
or (TUSBGenericDevice(Hub.Items[a]).Path <> '\\.\'+devname) then
begin
if Assigned(Hub.Items[a]) and (TUSBGenericDevice(Hub.Items[a]).Path <> '\\.\'+devname) then
TUSBGenericDevice(Hub.Items[a]).Free;
Hub.Items[a] := TUSBHub.Create('\\.\'+devname,Hub,Self,TUSBDeviceStatus(ConInfo.ConnectionStatus[0]));
hHub2 := CreateFile(pchar('\\.\'+devname),GENERIC_WRITE,FILE_SHARE_WRITE,@SA, OPEN_EXISTING, 0, 0);
if hHub2 <> INVALID_HANDLE_VALUE then
begin
if DeviceIoControl(hHub2,IOCTL_USB_GET_NODE_INFORMATION,@NodeInfo2, sizeof(NodeInfo2),@NodeInfo2, sizeof(NodeInfo2),BytesReturned, nil) then
begin
for b := 0 to NodeInfo2.HubDescriptor.bNumberOfPorts-1 do
TUSBHub(Hub.Items[a]).Add(nil);
end;
end;
if Assigned(OnUSBArrival) then
OnUSBArrival(TUSBGenericDevice(Hub.Items[a]));
end;
EnumHubDevices(TUSBHub(Hub.Items[a]));
end
else
begin
if DeviceIoControl(hHub,IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,@ConnectedHub, sizeof(ConnectedHub),@ConnectedHub, sizeof(ConnectedHub),BytesReturned, nil) then
devname := UnicodeStrLen(ConnectedHub.ActualLength,ConnectedHub.NodeName);
if (not Assigned(Hub.Items[a]))
or (TUSBDevice(Hub.Items[a]).Driver <> devname) then
begin
if Assigned(Hub.Items[a]) and (TUSBDevice(Hub.Items[a]).Driver <> devname) then
begin
FDeviceList.Remove(Hub.Items[a]);
TUSBGenericDevice(Hub.Items[a]).Free;
end;
DeviceClass := GetUSBDeviceClass(ConInfo.DeviceDescriptor.idVendor[1] << 8+ConInfo.DeviceDescriptor.idVendor[0],ConInfo.DeviceDescriptor.idProduct[1] << 8+ConInfo.DeviceDescriptor.idProduct[0]);
Hub.Items[a] := DeviceClass.Create(devname,Hub,Self,TUSBDeviceStatus(ConInfo.ConnectionStatus[0]));
FDeviceList.Add(Hub.Items[a]);
if TUSBDevice(Hub.Items[a]).Status = dsConnected then
begin
with TUSBDevice(Hub.Items[a]) do
begin
FVendor := USB_GetDescrStr(hHub,a+1,USB_GetLangID(hHub,a+1),ConInfo.DeviceDescriptor.iManufacturer);
FDeviceDescription := USB_GetDescrStr(hHub,a+1,USB_GetLangID(hHub,a+1),ConInfo.DeviceDescriptor.iProduct);
FSerial := USB_GetDescrStr(hHub,a+1,USB_GetLangID(hHub,a+1),ConInfo.DeviceDescriptor.iSerialNumber);
FDeviceID := ConInfo.DeviceDescriptor.idProduct[1] << 8+ConInfo.DeviceDescriptor.idProduct[0];
FVendorID := ConInfo.DeviceDescriptor.idVendor[1] << 8+ConInfo.DeviceDescriptor.idVendor[0];
end;
end;
if Assigned(OnUSBArrival) then
OnUSBArrival(TUSBGenericDevice(Hub.Items[a]));
end;
end;
end;
end;
end;
end;
CloseHandle(hHub);
end;
end;
begin
for i := 0 to FBusList.Count-1 do
TUSBHostController(FBusList[i]).Tag := 0;
for HostNum := 0 to 9 do
begin
DevName := '\\.\HCD' + char(HostNum+ord('0'));
hHost := CreateFile(PChar(devname),GENERIC_WRITE,FILE_SHARE_WRITE,@SA, OPEN_EXISTING, 0, 0);
if hHost <> INVALID_HANDLE_VALUE then
begin
aHostController := nil;
for i := 0 to FBusList.Count-1 do
if TUSBHostController(FBusList[i]).Path = DevName then
begin
TUSBHostController(FBusList[i]).Tag := 1;
aHostController := TUSBHostController(FBusList[i]);
break;
end;
if not Assigned(aHostController) then
begin
aHostController := TUSBHostController.Create(DevName);
aHostController.Tag := 1;
FBusList.Add(aHostController);
end;
if DeviceIoControl(hHost,IOCTL_USB_GET_ROOT_HUB_NAME,@UnicodeName, sizeof(UnicodeName),@UnicodeName, sizeof(UnicodeName), BytesReturned, nil) then
begin
devname := UnicodeStrLen(UnicodeName.ActualLength,UnicodeName.HubName);
if (aHostController.Count > 0) and (aHostController.Devices[0].Path = '\\.\'+devname) then
aRootHub := TUSBHub(aHostController.Devices[0])
else
begin
aRootHub := TUSBHub.Create('\\.\'+devname,nil,Self,dsConnected);
aHostController.Add(aRootHub);
end;
EnumHubDevices(aRootHub)
end;
CloseHandle(hHost);
end;
end;
end;
function TUSBDevice.OpenDevice : Boolean;
begin
if FFileHandle = INVALID_HANDLE_VALUE then // if not already opened
begin
FFileHandle := CreateFile(PChar(FPath), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
FHasReadWriteAccess := FFileHandle <> INVALID_HANDLE_VALUE;
if not HasReadWriteAccess then
FFileHandle := CreateFile(PChar(FPath), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
end;
Result := FileHandle <> INVALID_HANDLE_VALUE;
end;
procedure TUSBDevice.CloseDevice;
begin
if FFileHandle <> INVALID_HANDLE_VALUE then
CloseHandle(FFileHandle);
FFileHandle := INVALID_HANDLE_VALUE;
end;
function CM_Get_Child(var dnChildInst: DWord;dnDevInst: DWord;ulFlags: LongWord): DWord; stdcall; external 'CFGMGR32';
{
CMAPI CONFIGRET WINAPI CM_Get_Child(var OUT PDEVINST pdnDevInst,
IN DEVINST dnDevInst,
IN ULONG ulFlags
);
}
constructor TUSBDevice.Create(aDeviceHandle: string;aParent : TUSBGenericDevice;aController : TUSBController;aStatus : TUSBDeviceStatus);
var
aBytesReturned: DWORD;
Success: Boolean;
ChildInst : DWORD;
const
cHidGuid: TGUID = '{4d1e55b2-f16f-11cf-88cb-001111000030}';
cRawUSBGuid: TGUID = '{a5dcbf10-6530-11d2-901f-00c04fb951ed}';
cComPortGuid : TGUID = '{86e0d1e0-8089-11d0-9ce4-08003e301f73}';
function GetRegistryPropertyString(PnPHandle: HDEVINFO; const DevData: PSP_DEVINFO_DATA; Prop: DWORD): shortstring;
var
RegDataType: DWORD;
Buffer: array [0..256] of Char;
begin
aBytesReturned := 0;
RegDataType := 0;
Buffer[0] := #0;
SetupDiGetDeviceRegistryPropertyA(PnPHandle, DevData, Prop, @RegDataType, PBYTE(@Buffer[0]), SizeOf(Buffer), @aBytesReturned);
Result := PChar(Buffer);
end;
function GetDevicePathFromDriverKey(DriverKey : string;GUID : TGUID) : string;
var
PnPHandle: HDEVINFO;
DevData: SP_DEVINFO_DATA;
DeviceInterfaceData: SP_DEVICE_INTERFACE_DATA;
FunctionClassDeviceData: ^SP_DEVICE_INTERFACE_DETAIL_DATA_A;
Success: LongBool;
Devn: Integer;
tmp: String;
Flags: LongWord;
begin
PnPHandle := SetupDiGetClassDevsA(@Guid, nil, 0,DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if PnPHandle = Pointer(INVALID_HANDLE_VALUE) then
Exit;
Devn := 0;
repeat
DeviceInterfaceData.cbSize := SizeOf(SP_DEVICE_INTERFACE_DATA);
Success := SetupDiEnumDeviceInterfaces(PnPHandle, nil, @Guid, Devn, @DeviceInterfaceData);
if Success then
begin
DevData.cbSize := SizeOf(DevData);
aBytesReturned := 0;
SetupDiGetDeviceInterfaceDetailA(PnPHandle, @DeviceInterfaceData, nil, 0, @aBytesReturned, @DevData);
if (aBytesReturned <> 0) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
FunctionClassDeviceData := AllocMem(aBytesReturned);
FunctionClassDeviceData^.cbSize := 5;
if SetupDiGetDeviceInterfaceDetailA(PnPHandle, @DeviceInterfaceData, FunctionClassDeviceData, aBytesReturned, @aBytesReturned, @DevData) then
begin
if GetRegistryPropertyString(PnPHandle,@DevData,SPDRP_DRIVER) = DriverKey then
begin
if CM_Get_Child(ChildInst,DevData.DevInst,0) <> {CR_SUCCESS}0 then
ChildInst := 0;
Result := PChar(@FunctionClassDeviceData^.DevicePath);
end;
end;
FreeMem(FunctionClassDeviceData);
end;
Inc(Devn);
end;
until not Success;
SetupDiDestroyDeviceInfoList(PnPHandle);
end;
function GetComPortFromChild(GUID : TGUID) : string;
var
PnPHandle: HDEVINFO;
DevData: SP_DEVINFO_DATA;
DeviceInterfaceData: SP_DEVICE_INTERFACE_DATA;
FunctionClassDeviceData: ^SP_DEVICE_INTERFACE_DETAIL_DATA_A;
Success: LongBool;
Devn: Integer;
tmp: String;
Flags: LongWord;
begin
PnPHandle := SetupDiGetClassDevsA(@Guid, nil, 0,DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if PnPHandle = Pointer(INVALID_HANDLE_VALUE) then
Exit;
Devn := 0;
repeat
DeviceInterfaceData.cbSize := SizeOf(SP_DEVICE_INTERFACE_DATA);
Success := SetupDiEnumDeviceInterfaces(PnPHandle, nil, @Guid, Devn, @DeviceInterfaceData);
if Success then
begin
DevData.cbSize := SizeOf(DevData);
aBytesReturned := 0;
SetupDiGetDeviceInterfaceDetailA(PnPHandle, @DeviceInterfaceData, nil, 0, @aBytesReturned, @DevData);
if (aBytesReturned <> 0) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
FunctionClassDeviceData := AllocMem(aBytesReturned);
FunctionClassDeviceData^.cbSize := 5;
if SetupDiGetDeviceInterfaceDetailA(PnPHandle, @DeviceInterfaceData, FunctionClassDeviceData, aBytesReturned, @aBytesReturned, @DevData) then
begin
if ChildInst = DevData.DevInst then
Result := GetRegistryPropertyString(PnPHandle,@DevData,SPDRP_FRIENDLYNAME);
end;
FreeMem(FunctionClassDeviceData);
end;
Inc(Devn);
end;
until not Success;
SetupDiDestroyDeviceInfoList(PnPHandle);
end;
begin
inherited Create(aDeviceHandle,aParent,aController,aStatus);
FFileHandle := INVALID_HANDLE_VALUE;
FDriver := aDeviceHandle;
FPath := GetDevicePathFromDriverKey(aDeviceHandle,cRawUSBGuid);
if ChildInst <> 0 then
FUSBSerialPort := GetComPortFromChild(cComPortGuid);
if not OpenDevice then exit;
CloseDevice;
end;
destructor TUSBDevice.Destroy;
begin
if Assigned(Controller.OnUSBRemove) then
Controller.OnUSBRemove(Self);
inherited Destroy;
end;