-
Notifications
You must be signed in to change notification settings - Fork 1
/
cyapi.h
468 lines (358 loc) · 13.4 KB
/
cyapi.h
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
//______________________________________________________________________________
//
// Copyright (c) Cypress Semiconductor, 2003
// All rights reserved.
//
//______________________________________________________________________________
#ifndef CyUSBH
#define CyUSBH
#include "windows.h"
#include "rpc.h"
#include <QUuid>
// From usb100.h
#ifndef __USB100_H__
#define __USB100_H__
#pragma pack(push,1)
typedef struct _USB_DEVICE_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT bcdUSB;
UCHAR bDeviceClass;
UCHAR bDeviceSubClass;
UCHAR bDeviceProtocol;
UCHAR bMaxPacketSize0;
USHORT idVendor;
USHORT idProduct;
USHORT bcdDevice;
UCHAR iManufacturer;
UCHAR iProduct;
UCHAR iSerialNumber;
UCHAR bNumConfigurations;
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
typedef struct _USB_ENDPOINT_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
UCHAR bEndpointAddress;
UCHAR bmAttributes;
USHORT wMaxPacketSize;
UCHAR bInterval;
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
typedef struct _USB_CONFIGURATION_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT wTotalLength;
UCHAR bNumInterfaces;
UCHAR bConfigurationValue;
UCHAR iConfiguration;
UCHAR bmAttributes;
UCHAR MaxPower;
} USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
typedef struct _USB_INTERFACE_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
UCHAR bInterfaceNumber;
UCHAR bAlternateSetting;
UCHAR bNumEndpoints;
UCHAR bInterfaceClass;
UCHAR bInterfaceSubClass;
UCHAR bInterfaceProtocol;
UCHAR iInterface;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
typedef struct _USB_STRING_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
WCHAR bString[1];
} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
typedef struct _USB_COMMON_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
} USB_COMMON_DESCRIPTOR, *PUSB_COMMON_DESCRIPTOR;
#pragma pack(pop)
#endif
//______________________________________________________________________________
class CCyIsoPktInfo {
public:
LONG Status;
LONG Length;
};
//______________________________________________________________________________
// {AE18AA60-7F6A-11d4-97DD-00010229B959}
//static GUID CYUSBDRV_GUID = {0xae18aa60, 0x7f6a, 0x11d4, 0x97, 0xdd, 0x0, 0x1, 0x2, 0x29, 0xb9, 0x59};
//QUuid temp()
static GUID GUID_KNJN_FX2 = {0x0EFA2C93, 0x0C7B, 0x454F, 0x94, 0x03, 0xD6, 0x38, 0xF6, 0xC3, 0x7E, 0x65};
typedef enum {TGT_DEVICE, TGT_INTFC, TGT_ENDPT, TGT_OTHER } CTL_XFER_TGT_TYPE;
typedef enum {REQ_STD, REQ_CLASS, REQ_VENDOR } CTL_XFER_REQ_TYPE;
typedef enum {DIR_TO_DEVICE, DIR_FROM_DEVICE } CTL_XFER_DIR_TYPE;
typedef enum {XMODE_BUFFERED, XMODE_DIRECT } XFER_MODE_TYPE;
const int MAX_ENDPTS = 16;
const int MAX_INTERFACES = 8;
const int USB_STRING_MAXLEN = 256;
////////////////////////////////////////////////////////////////////////////////
//
// The CCyEndPoint ABSTRACT Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyUSBEndPoint
{
protected:
bool WaitForIO(OVERLAPPED *ovLapStatus);
virtual PUCHAR BeginDirectXfer(PUCHAR buf, LONG bufLen, OVERLAPPED *ov);
virtual PUCHAR BeginBufferedXfer(PUCHAR buf, LONG bufLen, OVERLAPPED *ov);
public:
CCyUSBEndPoint(void);
CCyUSBEndPoint(CCyUSBEndPoint& ept);
CCyUSBEndPoint(HANDLE h, PUSB_ENDPOINT_DESCRIPTOR pEndPtDescriptor);
HANDLE hDevice;
// The fields of an EndPoint Descriptor
UCHAR DscLen;
UCHAR DscType;
UCHAR Address;
UCHAR Attributes;
USHORT MaxPktSize;
USHORT PktsPerFrame;
UCHAR Interval;
// Other fields
ULONG TimeOut;
ULONG UsbdStatus;
ULONG NtStatus;
DWORD bytesWritten;
DWORD LastError;
bool bIn;
XFER_MODE_TYPE XferMode;
bool XferData(PUCHAR buf, LONG &len, CCyIsoPktInfo* pktInfos = NULL);
virtual PUCHAR BeginDataXfer(PUCHAR buf, LONG len, OVERLAPPED *ov) = 0;
virtual bool FinishDataXfer(PUCHAR buf, LONG &len, OVERLAPPED *ov, PUCHAR pXmitBuf, CCyIsoPktInfo* pktInfos = NULL);
bool WaitForXfer(OVERLAPPED *ov, ULONG tOut);
ULONG GetXferSize(void);
void SetXferSize(ULONG xfer);
bool Reset(void);
bool Abort(void);
private:
};
////////////////////////////////////////////////////////////////////////////////
//
// The Control Endpoint Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyControlEndPoint : public CCyUSBEndPoint
{
private:
public:
CCyControlEndPoint(void);
CCyControlEndPoint(CCyControlEndPoint& ept);
CCyControlEndPoint(HANDLE h, PUSB_ENDPOINT_DESCRIPTOR pEndPtDescriptor);
CTL_XFER_TGT_TYPE Target;
CTL_XFER_REQ_TYPE ReqType;
CTL_XFER_DIR_TYPE Direction;
UCHAR ReqCode;
WORD Value;
WORD Index;
bool Read(PUCHAR buf, LONG &len);
bool Write(PUCHAR buf, LONG &len);
PUCHAR BeginDataXfer(PUCHAR buf, LONG len, OVERLAPPED *ov);
};
////////////////////////////////////////////////////////////////////////////////
//
// The Isoc Endpoint Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyIsocEndPoint : public CCyUSBEndPoint
{
protected:
virtual PUCHAR BeginDirectXfer(PUCHAR buf, LONG bufLen, OVERLAPPED *ov);
virtual PUCHAR BeginBufferedXfer(PUCHAR buf, LONG bufLen, OVERLAPPED *ov);
public:
CCyIsocEndPoint(void);
CCyIsocEndPoint(HANDLE h, PUSB_ENDPOINT_DESCRIPTOR pEndPtDescriptor);
PUCHAR BeginDataXfer(PUCHAR buf, LONG len, OVERLAPPED *ov);
CCyIsoPktInfo* CreatePktInfos(LONG bufLen, int &packets);
};
////////////////////////////////////////////////////////////////////////////////
//
// The Bulk Endpoint Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyBulkEndPoint : public CCyUSBEndPoint
{
public:
CCyBulkEndPoint(void);
CCyBulkEndPoint(HANDLE h, PUSB_ENDPOINT_DESCRIPTOR pEndPtDescriptor);
PUCHAR BeginDataXfer(PUCHAR buf, LONG len, OVERLAPPED *ov);
};
////////////////////////////////////////////////////////////////////////////////
//
// The Interrupt Endpoint Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyInterruptEndPoint : public CCyUSBEndPoint
{
public:
CCyInterruptEndPoint(void);
CCyInterruptEndPoint(HANDLE h, PUSB_ENDPOINT_DESCRIPTOR pEndPtDescriptor);
PUCHAR BeginDataXfer(PUCHAR buf, LONG len, OVERLAPPED *ov);
};
////////////////////////////////////////////////////////////////////////////////
//
// The Interface Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyUSBInterface
{
private:
protected:
public:
CCyUSBEndPoint *EndPoints[MAX_ENDPTS]; // Holds pointers to all the interface's endpoints, plus a pointer to the Control endpoint zero
UCHAR bLength;
UCHAR bDescriptorType;
UCHAR bInterfaceNumber;
UCHAR bAlternateSetting;
UCHAR bNumEndpoints; // Not counting the control endpoint
UCHAR bInterfaceClass;
UCHAR bInterfaceSubClass;
UCHAR bInterfaceProtocol;
UCHAR iInterface;
UCHAR bAltSettings;
USHORT wTotalLength; // Needed in case Intfc has additional (non-endpt) descriptors
CCyUSBInterface(HANDLE h, PUSB_INTERFACE_DESCRIPTOR pIntfcDescriptor);
CCyUSBInterface(CCyUSBInterface& ifc); // Copy Constructor
~CCyUSBInterface(void);
};
////////////////////////////////////////////////////////////////////////////////
//
// The Config Class
//
////////////////////////////////////////////////////////////////////////////////
class CCyUSBConfig
{
private:
protected:
public:
CCyUSBInterface *Interfaces[MAX_INTERFACES];
UCHAR bLength;
UCHAR bDescriptorType;
USHORT wTotalLength;
UCHAR bNumInterfaces;
UCHAR bConfigurationValue;
UCHAR iConfiguration;
UCHAR bmAttributes;
UCHAR MaxPower;
UCHAR AltInterfaces;
CCyUSBConfig(void);
CCyUSBConfig(CCyUSBConfig& cfg); // Copy Constructor
CCyUSBConfig(HANDLE h, PUSB_CONFIGURATION_DESCRIPTOR pConfigDescr);
~CCyUSBConfig(void);
};
////////////////////////////////////////////////////////////////////////////////
//
// The USB Device Class - This is the main class that contains members of all the
// other classes.
//
// To use the library, create an instance of this Class and call it's Open method
//
////////////////////////////////////////////////////////////////////////////////
class CCyUSBDevice
{
// The public members are accessible (i.e. corruptible) by the user of the library
// Algorithms of the class don't rely on any public members. Instead, they use the
// private members of the class for their calculations.
public:
CCyUSBDevice(HANDLE hnd = NULL, GUID guid = GUID_KNJN_FX2, BOOL bOpen = true);
~CCyUSBDevice(void);
CCyUSBEndPoint **EndPoints; // Shortcut to USBCfgs[CfgNum]->Interfaces[IntfcIndex]->Endpoints
CCyUSBEndPoint *EndPointOf(UCHAR addr);
CCyControlEndPoint *ControlEndPt;
CCyIsocEndPoint *IsocInEndPt;
CCyIsocEndPoint *IsocOutEndPt;
CCyBulkEndPoint *BulkInEndPt;
CCyBulkEndPoint *BulkOutEndPt;
CCyInterruptEndPoint *InterruptInEndPt;
CCyInterruptEndPoint *InterruptOutEndPt;
USHORT StrLangID;
ULONG UsbdStatus;
ULONG NtStatus;
ULONG DriverVersion;
ULONG USBDIVersion;
char DeviceName[USB_STRING_MAXLEN];
char FriendlyName[USB_STRING_MAXLEN];
wchar_t Manufacturer[USB_STRING_MAXLEN];
wchar_t Product[USB_STRING_MAXLEN];
wchar_t SerialNumber[USB_STRING_MAXLEN];
CHAR DevPath[USB_STRING_MAXLEN];
USHORT BcdUSB;
USHORT VendorID;
USHORT ProductID;
UCHAR USBAddress;
UCHAR DevClass;
UCHAR DevSubClass;
UCHAR DevProtocol;
UCHAR MaxPacketSize;
USHORT BcdDevice;
UCHAR ConfigValue;
UCHAR ConfigAttrib;
UCHAR MaxPower;
UCHAR IntfcClass;
UCHAR IntfcSubClass;
UCHAR IntfcProtocol;
DWORD BytesXfered;
UCHAR DeviceCount(void);
UCHAR ConfigCount(void);
UCHAR IntfcCount(void);
UCHAR AltIntfcCount(void);
UCHAR EndPointCount(void);
UCHAR Config(void) { return CfgNum; } // Normally 0
void SetConfig(UCHAR cfg);
UCHAR Interface(void) { return IntfcNum; } // Usually 0
// No SetInterface method since only 1 intfc per device (per Windows)
UCHAR AltIntfc(void);
bool SetAltIntfc(UCHAR alt);
GUID DriverGUID(void) { return DrvGuid; }
HANDLE DeviceHandle(void) { return hDevice; }
void UsbdStatusString(ULONG stat, PCHAR s);
bool CreateHandle(UCHAR dev);
void DestroyHandle();
bool Open(UCHAR dev);
void Close(void);
bool Reset(void);
bool ReConnect(void);
bool Suspend(void);
bool Resume(void);
bool IsOpen(void) { return (hDevice != INVALID_HANDLE_VALUE); }
UCHAR PowerState(void);
void GetDeviceDescriptor(PUSB_DEVICE_DESCRIPTOR descr);
void GetConfigDescriptor(PUSB_CONFIGURATION_DESCRIPTOR descr);
void GetIntfcDescriptor(PUSB_INTERFACE_DESCRIPTOR descr);
CCyUSBConfig GetUSBConfig(int index);
private:
USB_DEVICE_DESCRIPTOR USBDeviceDescriptor;
PUSB_CONFIGURATION_DESCRIPTOR USBConfigDescriptors[2];
CCyUSBConfig *USBCfgs[2];
HANDLE hWnd;
HANDLE hDevice;
HANDLE hDevNotification;
HANDLE hHndNotification;
GUID DrvGuid;
UCHAR Devices;
UCHAR Interfaces;
UCHAR AltInterfaces;
UCHAR Configs;
UCHAR DevNum;
UCHAR CfgNum;
UCHAR IntfcNum; // The current selected interface's bInterfaceNumber
UCHAR IntfcIndex; // The entry in the Config's interfaces table matching to IntfcNum and AltSetting
void GetDevDescriptor(void);
void GetCfgDescriptor(int descIndex);
void GetString(wchar_t *s, UCHAR sIndex);
void SetStringDescrLanguage(void);
void SetAltIntfcParams(UCHAR alt);
bool IoControl(ULONG cmd, PUCHAR buf, ULONG len);
void SetEndPointPtrs(void);
void GetDeviceName(void);
void GetFriendlyName(void);
void GetDriverVer(void);
void GetUSBDIVer(void);
void GetUSBAddress(void);
//void CloseEndPtHandles(void);
bool RegisterForPnpEvents(HANDLE h);
};
//---------------------------------------------------------------------------
#endif