-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadusb.h
458 lines (373 loc) · 14.5 KB
/
loadusb.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
// Copyright (c) <2012> <Leif Asbrink>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
// this file contains definitions from libusb-win32 (usb.h),
// libusb-1.0 (libusb.h)
// The complete packages can be downloaded here:
// http://sm5bsz.com/linuxdsp/install/compile/wincompile_nasmplus.htm
#include "globdef.h"
#if OSNUM == OSNUM_LINUX
#include <limits.h>
#define LIBUSB_CALL
#endif
#if OSNUM == OSNUM_WINDOWS
#include <windows.h>
#include <pshpack1.h>
#include <poppack.h>
#ifdef interface
#undef interface
#endif
#define LIBUSB_CALL WINAPI
#endif
struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;
struct usb_device;
struct usb_bus;
int usbGetStringAscii(usb_dev_handle *dev, int my_index,
int langid, char *buf, int buflen);
int select_libusb_version(char* name, char* lib0, char* lib1);
#define USB_TYPE_VENDOR (0x02 << 5)
#define USB_RECIP_DEVICE 0x00
#define USB_ENDPOINT_IN 0x80
#define USB_TYPE_VENDOR (0x02 << 5)
#define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_DT_STRING 0x03
#define USB_ENDPOINT_OUT 0x00
/** \ingroup misc
* Recipient bits of the
* \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
* transfers. Values 4 through 31 are reserved. */
enum libusb_request_recipient {
/** Device */
LIBUSB_RECIPIENT_DEVICE = 0x00,
/** Interface */
LIBUSB_RECIPIENT_INTERFACE = 0x01,
/** Endpoint */
LIBUSB_RECIPIENT_ENDPOINT = 0x02,
/** Other */
LIBUSB_RECIPIENT_OTHER = 0x03,
};
/** \ingroup misc
* Request type bits of the
* \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
* transfers. */
enum libusb_request_type {
/** Standard */
LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
/** Class */
LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
/** Vendor */
LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
/** Reserved */
LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
};
/** \ingroup desc
* Endpoint direction. Values for bit 7 of the
* \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
*/
enum libusb_endpoint_direction {
/** In: device-to-host */
LIBUSB_ENDPOINT_IN = 0x80,
/** Out: host-to-device */
LIBUSB_ENDPOINT_OUT = 0x00
};
/** \ingroup desc
* A structure representing the standard USB device descriptor. This
* descriptor is documented in section 9.6.1 of the USB 2.0 specification.
* All multiple-byte fields are represented in host-endian format.
*/
struct libusb_device_descriptor {
/** Size of this descriptor (in bytes) */
uint8_t bLength;
/** Descriptor type. Will have value
* \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
* context. */
uint8_t bDescriptorType;
/** USB specification release number in binary-coded decimal. A value of
* 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
uint16_t bcdUSB;
/** USB-IF class code for the device. See \ref libusb_class_code. */
uint8_t bDeviceClass;
/** USB-IF subclass code for the device, qualified by the bDeviceClass
* value */
uint8_t bDeviceSubClass;
/** USB-IF protocol code for the device, qualified by the bDeviceClass and
* bDeviceSubClass values */
uint8_t bDeviceProtocol;
/** Maximum packet size for endpoint 0 */
uint8_t bMaxPacketSize0;
/** USB-IF vendor ID */
uint16_t idVendor;
/** USB-IF product ID */
uint16_t idProduct;
/** Device release number in binary-coded decimal */
uint16_t bcdDevice;
/** Index of string descriptor describing manufacturer */
uint8_t iManufacturer;
/** Index of string descriptor describing product */
uint8_t iProduct;
/** Index of string descriptor containing device serial number */
uint8_t iSerialNumber;
/** Number of possible configurations */
uint8_t bNumConfigurations;
};
struct libusb_context;
struct libusb_device;
struct libusb_device_handle;
typedef struct libusb_context libusb_context;
typedef struct libusb_device libusb_device;
typedef struct libusb_device_handle libusb_device_handle;
/** \ingroup asyncio
* Transfer status codes */
enum libusb_transfer_status {
/** Transfer completed without error. Note that this does not indicate
* that the entire amount of requested data was transferred. */
LIBUSB_TRANSFER_COMPLETED,
/** Transfer failed */
LIBUSB_TRANSFER_ERROR,
/** Transfer timed out */
LIBUSB_TRANSFER_TIMED_OUT,
/** Transfer was cancelled */
LIBUSB_TRANSFER_CANCELLED,
/** For bulk/interrupt endpoints: halt condition detected (endpoint
* stalled). For control endpoints: control request not supported. */
LIBUSB_TRANSFER_STALL,
/** Device was disconnected */
LIBUSB_TRANSFER_NO_DEVICE,
/** Device sent more data than requested */
LIBUSB_TRANSFER_OVERFLOW,
};
// Endpoint transfer type. Values for bits 0:1 of the
// \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
enum libusb_transfer_type {
/** Control endpoint */
LIBUSB_TRANSFER_TYPE_CONTROL = 0,
/** Isochronous endpoint */
LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
/** Bulk endpoint */
LIBUSB_TRANSFER_TYPE_BULK = 2,
/** Interrupt endpoint */
LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
/** Stream endpoint */
LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4,
};
/** \ingroup asyncio
* Isochronous packet descriptor. */
struct libusb_iso_packet_descriptor {
/** Length of data to request in this packet */
unsigned int length;
/** Amount of data that was actually transferred */
unsigned int actual_length;
/** Status code for this packet */
enum libusb_transfer_status status;
};
struct libusb_transfer;
typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
/** \ingroup asyncio
* The generic USB transfer structure. The user populates this structure and
* then submits it in order to request a transfer. After the transfer has
* completed, the library populates the transfer with the results and passes
* it back to the user.
*/
struct libusb_transfer {
/** Handle of the device that this transfer will be submitted to */
libusb_device_handle *dev_handle;
/** A bitwise OR combination of \ref libusb_transfer_flags. */
uint8_t flags;
/** Address of the endpoint where this transfer will be sent. */
unsigned char endpoint;
/** Type of the endpoint from \ref libusb_transfer_type */
unsigned char type;
/** Timeout for this transfer in millseconds. A value of 0 indicates no
* timeout. */
unsigned int timeout;
/** The status of the transfer. Read-only, and only for use within
* transfer callback function.
*
* If this is an isochronous transfer, this field may read COMPLETED even
* if there were errors in the frames. Use the
* \ref libusb_iso_packet_descriptor::status "status" field in each packet
* to determine if errors occurred. */
enum libusb_transfer_status status;
/** Length of the data buffer */
int length;
/** Actual length of data that was transferred. Read-only, and only for
* use within transfer callback function. Not valid for isochronous
* endpoint transfers. */
int actual_length;
/** Callback function. This will be invoked when the transfer completes,
* fails, or is cancelled. */
libusb_transfer_cb_fn callback;
/** User context data to pass to the callback function. */
void *user_data;
/** Data buffer */
unsigned char *buffer;
/** Number of isochronous packets. Only used for I/O with isochronous
* endpoints. */
int num_iso_packets;
/** Isochronous packet descriptors, for isochronous transfers only. */
struct libusb_iso_packet_descriptor iso_packet_desc
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
[] /* valid C99 code */
#else
[0] /* non-standard, but usually working code */
#endif
;
};
static __inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
libusb_device_handle *dev_handle, unsigned char endpoint,
unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
void *user_data, unsigned int timeout)
{
transfer->dev_handle = dev_handle;
transfer->endpoint = endpoint;
transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
transfer->timeout = timeout;
transfer->buffer = buffer;
transfer->length = length;
transfer->user_data = user_data;
transfer->callback = callback;
}
typedef int (LIBUSB_CALL *p_libusb_get_string_descriptor_ascii)
(libusb_device_handle *dev,
uint8_t desc_index, unsigned char *data, int length);
typedef void (LIBUSB_CALL *p_libusb_close)(libusb_device_handle *dev_handle);
typedef int (LIBUSB_CALL *p_libusb_control_transfer)(libusb_device_handle *dev_handle,
uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
unsigned char *data, uint16_t wLength, unsigned int timeout);
typedef int (LIBUSB_CALL *p_libusb_open)(libusb_device *dev, libusb_device_handle **handle);
typedef int (LIBUSB_CALL *p_libusb_init)(libusb_context **ctx);
typedef intptr_t (LIBUSB_CALL *p_libusb_get_device_list)(libusb_context *ctx,
libusb_device ***list);
typedef int (LIBUSB_CALL *p_libusb_get_device_descriptor)(libusb_device *dev,
struct libusb_device_descriptor *desc);
typedef void (LIBUSB_CALL *p_libusb_free_device_list)(libusb_device **list,
int unref_devices);
// --
typedef int (LIBUSB_CALL *p_libusb_submit_transfer)(struct libusb_transfer *transfer);
typedef int (LIBUSB_CALL *p_libusb_reset_device)(libusb_device_handle *dev);
typedef void (LIBUSB_CALL *p_libusb_exit)(libusb_context *ctx);
typedef int (LIBUSB_CALL *p_libusb_bulk_transfer)(libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length,
int *actual_length, unsigned int timeout);
typedef int (LIBUSB_CALL *p_libusb_cancel_transfer)(struct libusb_transfer *transfer);
typedef int (LIBUSB_CALL *p_libusb_handle_events_timeout)(libusb_context *ctx,
struct timeval *tv);
typedef int (LIBUSB_CALL *p_libusb_release_interface)(libusb_device_handle *dev,
int interface_number);
typedef int (LIBUSB_CALL *p_libusb_claim_interface)(libusb_device_handle *dev,
int interface_number);
typedef void (LIBUSB_CALL *p_libusb_free_transfer)(struct libusb_transfer *transfer);
typedef int (LIBUSB_CALL *p_libusb_set_interface_alt_setting)(libusb_device_handle *dev,
int interface_number, int alternate_setting);
typedef struct libusb_transfer* (LIBUSB_CALL *p_libusb_alloc_transfer)(int iso_packets);
extern p_libusb_control_transfer libusb_control_transfer;
extern p_libusb_get_string_descriptor_ascii libusb_get_string_descriptor_ascii;
extern p_libusb_close libusb_close;
extern p_libusb_open libusb_open;
extern p_libusb_init libusb_init;
extern p_libusb_get_device_list libusb_get_device_list;
extern p_libusb_get_device_descriptor libusb_get_device_descriptor;
extern p_libusb_free_device_list libusb_free_device_list;
extern p_libusb_submit_transfer libusb_submit_transfer;
extern p_libusb_reset_device libusb_reset_device;
extern p_libusb_exit libusb_exit;
extern p_libusb_bulk_transfer libusb_bulk_transfer;
extern p_libusb_cancel_transfer libusb_cancel_transfer;
extern p_libusb_handle_events_timeout libusb_handle_events_timeout;
extern p_libusb_release_interface libusb_release_interface;
extern p_libusb_claim_interface libusb_claim_interface;
extern p_libusb_free_transfer libusb_free_transfer;
extern p_libusb_set_interface_alt_setting libusb_set_interface_alt_setting;
extern p_libusb_alloc_transfer libusb_alloc_transfer;
#if OSNUM == OSNUM_WINDOWS
#define LIBUSB_PATH_MAX 512
struct usb_device_descriptor {
unsigned char bLength;
unsigned char bDescriptorType;
unsigned short bcdUSB;
unsigned char bDeviceClass;
unsigned char bDeviceSubClass;
unsigned char bDeviceProtocol;
unsigned char bMaxPacketSize0;
unsigned short idVendor;
unsigned short idProduct;
unsigned short bcdDevice;
unsigned char iManufacturer;
unsigned char iProduct;
unsigned char iSerialNumber;
unsigned char bNumConfigurations;
};
struct usb_device {
struct usb_device *next, *prev;
char filename[LIBUSB_PATH_MAX];
struct usb_bus *bus;
struct usb_device_descriptor descriptor;
struct usb_config_descriptor *config;
void *dev;
unsigned char devnum;
unsigned char num_children;
struct usb_device **children;
};
struct usb_bus {
struct usb_bus *next, *prev;
char dirname[LIBUSB_PATH_MAX];
struct usb_device *devices;
unsigned long long location;
struct usb_device *root_dev;
};
#endif
#if OSNUM == OSNUM_LINUX
struct usb_device_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} __attribute__ ((packed));
struct usb_device {
struct usb_device *next, *prev;
char filename[PATH_MAX + 1];
struct usb_bus *bus;
struct usb_device_descriptor descriptor;
struct usb_config_descriptor *config;
void *dev; /* Darwin support */
uint8_t devnum;
unsigned char num_children;
struct usb_device **children;
};
struct usb_bus {
struct usb_bus *next, *prev;
char dirname[PATH_MAX + 1];
struct usb_device *devices;
uint32_t location;
struct usb_device *root_dev;
};
#endif