-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusbip.h
More file actions
401 lines (344 loc) · 13.7 KB
/
Copy pathusbip.h
File metadata and controls
401 lines (344 loc) · 13.7 KB
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
/* ########################################################################
USBIP hardware emulation
########################################################################
Copyright (c) : 2016 Luis Claudio Gambôa Lopes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#ifdef LINUX
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
#else
#include<winsock.h>
#endif
//system headers independent
#include<errno.h>
#include<stdarg.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
//defines
#define TCP_SERV_PORT 3240
typedef struct sockaddr sockaddr;
//USB definitions
#define byte unsigned char
#define word unsigned short
// USB Descriptors
#define USB_DESCRIPTOR_DEVICE 0x01 // Device Descriptor.
#define USB_DESCRIPTOR_CONFIGURATION 0x02 // Configuration Descriptor.
#define USB_DESCRIPTOR_STRING 0x03 // String Descriptor.
#define USB_DESCRIPTOR_INTERFACE 0x04 // Interface Descriptor.
#define USB_DESCRIPTOR_ENDPOINT 0x05 // Endpoint Descriptor.
#define USB_DESCRIPTOR_DEVICE_QUALIFIER 0x06 // Device Qualifier.
typedef struct __attribute__ ((__packed__)) _USB_DEVICE_DESCRIPTOR
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // DEVICE descriptor type (USB_DESCRIPTOR_DEVICE).
word bcdUSB; // USB Spec Release Number (BCD).
byte bDeviceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bDeviceSubClass; // Subclass code (assigned by the USB-IF).
byte bDeviceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bMaxPacketSize0; // Maximum packet size for endpoint 0.
word idVendor; // Vendor ID (assigned by the USB-IF).
word idProduct; // Product ID (assigned by the manufacturer).
word bcdDevice; // Device release number (BCD).
byte iManufacturer; // Index of String Descriptor describing the manufacturer.
byte iProduct; // Index of String Descriptor describing the product.
byte iSerialNumber; // Index of String Descriptor with the device's serial number.
byte bNumConfigurations; // Number of possible configurations.
} USB_DEVICE_DESCRIPTOR;
typedef struct __attribute__ ((__packed__)) _USB_CONFIGURATION_DESCRIPTOR
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // CONFIGURATION descriptor type (USB_DESCRIPTOR_CONFIGURATION).
word wTotalLength; // Total length of all descriptors for this configuration.
byte bNumInterfaces; // Number of interfaces in this configuration.
byte bConfigurationValue; // Value of this configuration (1 based).
byte iConfiguration; // Index of String Descriptor describing the configuration.
byte bmAttributes; // Configuration characteristics.
byte bMaxPower; // Maximum power consumed by this configuration.
} USB_CONFIGURATION_DESCRIPTOR;
typedef struct __attribute__ ((__packed__)) _USB_INTERFACE_DESCRIPTOR
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // INTERFACE descriptor type (USB_DESCRIPTOR_INTERFACE).
byte bInterfaceNumber; // Number of this interface (0 based).
byte bAlternateSetting; // Value of this alternate interface setting.
byte bNumEndpoints; // Number of endpoints in this interface.
byte bInterfaceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bInterfaceSubClass; // Subclass code (assigned by the USB-IF).
byte bInterfaceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte iInterface; // Index of String Descriptor describing the interface.
} USB_INTERFACE_DESCRIPTOR;
typedef struct __attribute__ ((__packed__)) _USB_ENDPOINT_DESCRIPTOR
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // ENDPOINT descriptor type (USB_DESCRIPTOR_ENDPOINT).
byte bEndpointAddress; // Endpoint address. Bit 7 indicates direction (0=OUT, 1=IN).
byte bmAttributes; // Endpoint transfer type.
word wMaxPacketSize; // Maximum packet size.
byte bInterval; // Polling interval in frames.
} USB_ENDPOINT_DESCRIPTOR;
typedef struct __attribute__ ((__packed__)) _USB_DEVICE_QUALIFIER_DESCRIPTOR
{
byte bLength; // Size of this descriptor
byte bType; // Type, always USB_DESCRIPTOR_DEVICE_QUALIFIER
word bcdUSB; // USB spec version, in BCD
byte bDeviceClass; // Device class code
byte bDeviceSubClass; // Device sub-class code
byte bDeviceProtocol; // Device protocol
byte bMaxPacketSize0; // EP0, max packet size
byte bNumConfigurations; // Number of "other-speed" configurations
byte bReserved; // Always zero (0)
} USB_DEVICE_QUALIFIER_DESCRIPTOR;
//Generic Configuration
typedef struct __attribute__ ((__packed__)) _CONFIG_GEN
{
USB_CONFIGURATION_DESCRIPTOR dev_conf;
USB_INTERFACE_DESCRIPTOR dev_int;
} CONFIG_GEN;
//HID
typedef struct __attribute__ ((__packed__)) _USB_HID_DESCRIPTOR
{
byte bLength;
byte bDescriptorType;
word bcdHID;
byte bCountryCode;
byte bNumDescriptors;
byte bRPDescriptorType;
word wRPDescriptorLength;
} USB_HID_DESCRIPTOR;
//Configuration
typedef struct __attribute__ ((__packed__)) _CONFIG_HID
{
USB_CONFIGURATION_DESCRIPTOR dev_conf;
USB_INTERFACE_DESCRIPTOR dev_int;
USB_HID_DESCRIPTOR dev_hid;
USB_ENDPOINT_DESCRIPTOR dev_ep;
} CONFIG_HID;
//Configuration
typedef struct __attribute__ ((__packed__)) _CONFIG_MASS
{
USB_CONFIGURATION_DESCRIPTOR dev_conf;
USB_INTERFACE_DESCRIPTOR dev_int;
USB_ENDPOINT_DESCRIPTOR dev_ep_in;
USB_ENDPOINT_DESCRIPTOR dev_ep_out;
} CONFIG_MASS;
//CDC
/* Functional Descriptor Structure - See CDC Specification 1.1 for details */
/* Header Functional Descriptor */
typedef struct __attribute__ ((__packed__)) _USB_CDC_HEADER_FN_DSC
{
byte bFNLength;
byte bDscType;
byte bDscSubType;
word bcdCDC;
} USB_CDC_HEADER_FN_DSC;
/* Abstract Control Management Functional Descriptor */
typedef struct __attribute__ ((__packed__)) _USB_CDC_ACM_FN_DSC
{
byte bFNLength;
byte bDscType;
byte bDscSubType;
byte bmCapabilities;
} USB_CDC_ACM_FN_DSC;
/* Union Functional Descriptor */
typedef struct __attribute__ ((__packed__)) _USB_CDC_UNION_FN_DSC
{
byte bFNLength;
byte bDscType;
byte bDscSubType;
byte bMasterIntf;
byte bSaveIntf0;
} USB_CDC_UNION_FN_DSC;
/* Call Management Functional Descriptor */
typedef struct __attribute__ ((__packed__)) _USB_CDC_CALL_MGT_FN_DSC
{
byte bFNLength;
byte bDscType;
byte bDscSubType;
byte bmCapabilities;
byte bDataInterface;
} USB_CDC_CALL_MGT_FN_DSC;
//Configuration
typedef struct __attribute__ ((__packed__)) _CONFIG_CDC
{
USB_CONFIGURATION_DESCRIPTOR dev_conf0;
USB_INTERFACE_DESCRIPTOR dev_int0;
USB_CDC_HEADER_FN_DSC cdc_header;
USB_CDC_CALL_MGT_FN_DSC cdc_call_mgt;
USB_CDC_ACM_FN_DSC cdc_acm;
USB_CDC_UNION_FN_DSC cdc_union;
USB_ENDPOINT_DESCRIPTOR dev_ep0;
USB_INTERFACE_DESCRIPTOR dev_int1;
USB_ENDPOINT_DESCRIPTOR dev_ep1;
USB_ENDPOINT_DESCRIPTOR dev_ep2;
} CONFIG_CDC;
//=================================================================================
//USBIP data struct
typedef struct __attribute__ ((__packed__)) _OP_REQ_DEVLIST
{
word version;
word command;
int status;
} OP_REQ_DEVLIST;
typedef struct __attribute__ ((__packed__)) _OP_REP_DEVLIST_HEADER
{
word version;
word command;
int status;
int nExportedDevice;
}OP_REP_DEVLIST_HEADER;
//================= for each device
typedef struct __attribute__ ((__packed__)) _OP_REP_DEVLIST_DEVICE
{
char usbPath[256];
char busID[32];
int busnum;
int devnum;
int speed;
word idVendor;
word idProduct;
word bcdDevice;
byte bDeviceClass;
byte bDeviceSubClass;
byte bDeviceProtocol;
byte bConfigurationValue;
byte bNumConfigurations;
byte bNumInterfaces;
}OP_REP_DEVLIST_DEVICE;
//================== for each interface
typedef struct __attribute__ ((__packed__)) _OP_REP_DEVLIST_INTERFACE
{
byte bInterfaceClass;
byte bInterfaceSubClass;
byte bInterfaceProtocol;
byte padding;
}OP_REP_DEVLIST_INTERFACE;
typedef struct __attribute__ ((__packed__)) _OP_REP_DEVLIST
{
OP_REP_DEVLIST_HEADER header;
OP_REP_DEVLIST_DEVICE device; //only one!
OP_REP_DEVLIST_INTERFACE *interfaces;
}OP_REP_DEVLIST;
typedef struct __attribute__ ((__packed__)) _OP_REQ_IMPORT
{
word version;
word command;
int status;
char busID[32];
}OP_REQ_IMPORT;
typedef struct __attribute__ ((__packed__)) _OP_REP_IMPORT
{
word version;
word command;
int status;
//------------- if not ok, finish here
char usbPath[256];
char busID[32];
int busnum;
int devnum;
int speed;
word idVendor;
word idProduct;
word bcdDevice;
byte bDeviceClass;
byte bDeviceSubClass;
byte bDeviceProtocol;
byte bConfigurationValue;
byte bNumConfigurations;
byte bNumInterfaces;
}OP_REP_IMPORT;
typedef struct __attribute__ ((__packed__)) _USBIP_CMD_SUBMIT
{
int command;
int seqnum;
int devid;
int direction;
int ep;
int transfer_flags;
int transfer_buffer_length;
int start_frame;
int number_of_packets;
int interval;
long long setup;
}USBIP_CMD_SUBMIT;
/*
+ Allowed transfer_flags | value | control | interrupt | bulk | isochronous
+ -------------------------+------------+---------+-----------+----------+-------------
+ URB_SHORT_NOT_OK | 0x00000001 | only in | only in | only in | no
+ URB_ISO_ASAP | 0x00000002 | no | no | no | yes
+ URB_NO_TRANSFER_DMA_MAP | 0x00000004 | yes | yes | yes | yes
+ URB_NO_FSBR | 0x00000020 | yes | no | no | no
+ URB_ZERO_PACKET | 0x00000040 | no | no | only out | no
+ URB_NO_INTERRUPT | 0x00000080 | yes | yes | yes | yes
+ URB_FREE_BUFFER | 0x00000100 | yes | yes | yes | yes
+ URB_DIR_MASK | 0x00000200 | yes | yes | yes | yes
*/
typedef struct __attribute__ ((__packed__)) _USBIP_RET_SUBMIT
{
int command;
int seqnum;
int devid;
int direction;
int ep;
int status;
int actual_length;
int start_frame;
int number_of_packets;
int error_count;
long long setup;
}USBIP_RET_SUBMIT;
typedef struct __attribute__ ((__packed__)) _USBIP_CMD_UNLINK
{
int command;
int seqnum;
int devid;
int direction;
int ep;
int seqnum_urb;
}USBIP_CMD_UNLINK;
typedef struct __attribute__ ((__packed__)) _USBIP_RET_UNLINK
{
int command;
int seqnum;
int devid;
int direction;
int ep;
int status;
}USBIP_RET_UNLINK;
typedef struct __attribute__ ((__packed__)) _StandardDeviceRequest
{
byte bmRequestType;
byte bRequest;
byte wValue0;
byte wValue1;
byte wIndex0;
byte wIndex1;
word wLength;
}StandardDeviceRequest;
void send_usb_req(int sockfd, USBIP_RET_SUBMIT * usb_req, char * data, unsigned int size, unsigned int status);
void usbip_run (const USB_DEVICE_DESCRIPTOR *dev_dsc);
//implemented by user
extern const USB_DEVICE_DESCRIPTOR dev_dsc;
extern const USB_DEVICE_QUALIFIER_DESCRIPTOR dev_qua;
extern const char * configuration;
extern const USB_INTERFACE_DESCRIPTOR *interfaces[];
extern const unsigned char *strings[];
void handle_data(int sockfd, USBIP_RET_SUBMIT *usb_req);
void handle_unknown_control(int sockfd, StandardDeviceRequest * control_req, USBIP_RET_SUBMIT *usb_req);