-
Notifications
You must be signed in to change notification settings - Fork 16
/
USBControlTransfers.h
407 lines (337 loc) · 8.76 KB
/
USBControlTransfers.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
#ifndef USB_CONTROL_TRANSFERS_H
#define USB_CONTROL_TRANSFERS_H
#include <map>
#include <LogicPublicTypes.h>
#include <AnalyzerResults.h>
#include "USBEnums.h"
struct USBRequest
{
U8 bmRequestType;
U8 bRequest;
U16 wValue;
U16 wIndex;
U16 wLength;
USBRequest()
{
Clear();
}
void Clear()
{
bmRequestType = 0;
bRequest = 0;
wValue = 0;
wIndex = 0;
wLength = 0;
}
void SetFromPacket( USBPacket& p );
U8 GetRequestedDescriptor() const
{
return wValue >> 8;
} // only valid when bRequest == GET_DESCRIPTOR
U8 GetRequestedDescriptorIndex() const
{
return wValue & 0xff;
} // only valid when bRequest == GET_DESCRIPTOR && GetRequestedDescriptor() == DT_STRING
bool IsStandardRequest() const
{
return ( bmRequestType & 0x60 ) == 0x00;
}
bool IsClassRequest() const
{
return ( bmRequestType & 0x60 ) == 0x20;
}
bool IsVendorRequest() const
{
return ( bmRequestType & 0x60 ) == 0x40;
}
bool IsRecipientDevice() const
{
return ( bmRequestType & 0x1f ) == 0x00;
}
bool IsRecipientInterface() const
{
return ( bmRequestType & 0x1f ) == 0x01;
}
bool IsRecipientEndpoint() const
{
return ( bmRequestType & 0x1f ) == 0x02;
}
bool IsRecipientOther() const
{
return ( bmRequestType & 0x1f ) == 0x03;
}
U8 GetInterfaceNum() const
{
return wValue & 0xff;
}
bool IsRequestedStandardDescriptor() const
{
return IsStandardRequest() && bRequest == GET_DESCRIPTOR && GetRequestedDescriptor() >= DT_DEVICE &&
GetRequestedDescriptor() <= DT_INTERFACE_POWER;
}
bool IsRequestedHIDReportDescriptor() const
{
return IsStandardRequest() && bRequest == GET_DESCRIPTOR && GetRequestedDescriptor() == DT_HID_REPORT;
}
};
/*
// Control transfer descriptor structures - these are here only for reference
struct USBDeviceDescriptor
{
U8 bLength;
U8 bDescriptorType;
U16 bcdUSB;
U8 bDeviceClass;
U8 bDeviceSubClass;
U8 bDeviceProtocol;
U8 bMaxPacketSize0;
U16 idVendor;
U16 idProduct;
U16 bcdDevice;
U8 iManufacturer;
U8 iProduct;
U8 iSerialNumber;
U8 bNumConfigurations;
};
struct USBConfigurationDescriptor
{
U8 bLength;
U8 bDescriptorType;
U16 wTotalLength;
U8 bNumInterfaces;
U8 bConfigurationValue;
U8 iConfiguration;
U8 bmAttributes;
U8 bMaxPower;
};
struct USBInterfaceDescriptor
{
U8 bLength;
U8 bDescriptorType;
U8 bInterfaceNumber;
U8 bAlternateSetting;
U8 bNumEndpoints;
U8 bInterfaceClass;
U8 bInterfaceSubClass;
U8 bInterfaceProtocol;
U8 iInterface;
};
struct USBEndpointDescriptor
{
U8 bLength;
U8 bDescriptorType;
U8 bEndpointAddress;
U8 bmAttributes;
U8 wMaxPacketSize;
U8 bInterval;
};
*/
class USBCtrlTransFieldFrame : public Frame
{
public:
USBCtrlTransFieldFrame()
{
mType = FT_ControlTransferField;
mFlags = FF_None;
}
void PackFrame( U32 data, U8 numBytes, U8 address, USBCtrlTransFieldType formatter, const char* name );
U32 GetData() const
{
return mData1 & 0xffffffff;
}
U8 GetNumBytes() const
{
return ( mData1 >> 32 ) & 0xff;
}
USBCtrlTransFieldType GetFormatter() const
{
return USBCtrlTransFieldType( ( mData1 >> 40 ) & 0xff );
}
U8 GetAddress() const
{
return ( mData1 >> 48 ) & 0xff;
}
const char* GetFieldName() const
{
return ( const char* )mData2;
}
};
class USBHidRepDescItemFrame : public Frame
{
public:
USBHidRepDescItemFrame()
{
mType = FT_HIDReportDescriptorItem;
mFlags = FF_None;
mData1 = mData2 = 0;
}
void PackFrame( const U8* pItem, U16 indentLevel, U16 usagePage );
const U8* GetItem() const
{
return ( const U8* )&mData1;
}
U16 GetIndentLevel() const
{
return mData2 & 0xffff;
}
U16 GetUsagePage() const
{
return ( U16 )( mData2 >> 16 );
}
};
// describes data structures
struct USBStructField
{
const char* name;
int numBytes;
USBCtrlTransFieldType formatter;
};
class USBControlTransferParser
{
private:
int mDescBytes; // size of this descriptor in bytes; equal to Descriptor.bLength
USBDescriptorType mDescType;
USBCDCDescriptorSubtype mDescSubtype;
int mParsedOffset; // number of bytes we have already parsed from the current descriptor
U32 mLeftover; // the LS byte of the previous packet in case a field spans two packets
USBAnalyzerResults* pResults;
U8 mAddress;
USBRequest mRequest;
std::string stringDescriptor;
// these are only valid during a ParsePacket call, and invalid before and after
USBPacket* pPacket;
int mPacketOffset; // index of the data byte of the current packet
int mPacketDataBytes; // number of bytes in the current packet
// we store the class IDs for the device and the interfaces
U8 mInterfaceNumber; // the last parsed interface number
typedef std::map<U8, USBClassCodes> USBInterfaceClassesContainer;
USBInterfaceClassesContainer mInterfaceClasses;
// used for the HID report descriptor parser
U8 mHidRepDescItem[ 5 ];
U8 mHidRepDescBytes;
int mHidIndentLevel;
int mHidItemCnt;
std::vector<U16> mHidUsagePageStack;
USBClassCodes GetClassCodeForInterface( U8 iface ) const;
bool IsCDCClassRequest() const;
bool ParseStringDescriptor();
void ParseStructure( USBStructField* descFields );
void ParseStandardDescriptor();
void SetHIDUsagePage( U16 usagePage )
{
if( mHidUsagePageStack.empty() )
mHidUsagePageStack.push_back( 0 );
mHidUsagePageStack.back() = usagePage;
}
U16 GetHIDUsagePage()
{
if( mHidUsagePageStack.empty() )
return 0;
return mHidUsagePageStack.back();
}
void PushHIDUsagePage()
{
mHidUsagePageStack.push_back( GetHIDUsagePage() );
}
void PopHIDUsagePage()
{
if( !mHidUsagePageStack.empty() )
mHidUsagePageStack.pop_back();
}
void ParseCDCDataStage();
void ParseHIDReportDescriptor();
void ParseUnknownResponse();
public:
USBControlTransferParser()
{
ResetParser();
}
void SetAnalyzerResults( USBAnalyzerResults* pres )
{
pResults = pres;
}
void ResetParser();
void SetAddress( U8 address )
{
mAddress = address;
}
void SetRequest( USBPacket& packet )
{
mRequest.SetFromPacket( packet );
}
void ParseDataPacket( USBPacket& packet );
U8 GetClassForInterface( U8 iface ) const
{
// get the interface class
USBInterfaceClassesContainer::const_iterator srch( mInterfaceClasses.find( iface ) );
if( srch != mInterfaceClasses.end() )
return srch->second;
return 0;
}
};
class USBControlTransferPacketHandler
{
private:
enum ControlTransferLastPacketReceived
{
// setup stage
CTS_SetupToken,
CTS_SetupData,
CTS_SetupAck,
// data stage
CTS_DataInToken,
CTS_DataOutToken,
CTS_DataInData,
CTS_DataOutData,
CTS_DataEnd,
// status stage
CTS_StatusInToken,
CTS_StatusOutToken,
CTS_StatusInDataEmpty,
CTS_StatusOutDataEmpty,
CTS_StatusInNAKed,
CTS_StatusOutDataNAKed,
CTS_StatusEnd,
};
// control transfer state
ControlTransferLastPacketReceived mCtrlTransLastReceived;
USBRequestCode mCtrlTransRequest;
bool mCtrlTransDeviceToHost;
USBControlTransferParser mCtrlTransParser;
int mAddress;
USBAnalyzerResults* mResults;
public:
void Init( USBAnalyzerResults* pResults, int addr );
U64 ResetControlTransferParser( USBPacket& pckt, USBFrameFlags flag = FF_UnexpectedPacket );
U64 HandleControlTransfer( USBPacket& pckt );
};
inline int GetNumHIDItemDataBytes( U8 firstByte )
{
U8 retVal = firstByte & 0x03;
return retVal == 3 ? 4 : retVal;
}
inline bool IsStandardDescriptor( U8 descType )
{
return descType >= DT_DEVICE && descType <= DT_INTERFACE_POWER;
}
inline bool IsHIDItemCollection( U8 firstByte )
{
return ( firstByte & 0xfc ) == 0xa0;
}
inline bool IsHIDItemEndCollection( U8 firstByte )
{
return ( firstByte & 0xfc ) == 0xc0;
}
inline bool IsHIDItemPush( U8 firstByte )
{
return ( firstByte & 0xfc ) == 0xa4;
}
inline bool IsHIDItemPop( U8 firstByte )
{
return ( firstByte & 0xfc ) == 0xb4;
}
inline bool IsHIDItemUsagePage( U8 firstByte )
{
return ( firstByte & 0xfc ) == 0x04;
}
#endif // USB_CONTROL_TRANSFERS_H