libusb.h: Base Class EFh (Miscellaneous) was missing in class definition.
[platform/upstream/libusb.git] / libusb / libusb.h
1 /*
2  * Public libusb header file
3  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
4  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
5  * Copyright © 2012 Pete Batard <pete@akeo.ie>
6  * Copyright © 2012-2018 Nathan Hjelm <hjelmn@cs.unm.edu>
7  * Copyright © 2014-2020 Chris Dickens <christopher.a.dickens@gmail.com>
8  * For more information, please visit: http://libusb.info
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #ifndef LIBUSB_H
26 #define LIBUSB_H
27
28 #if defined(_MSC_VER)
29 /* on MS environments, the inline keyword is available in C++ only */
30 #if !defined(__cplusplus)
31 #define inline __inline
32 #endif
33 /* ssize_t is also not available */
34 #include <basetsd.h>
35 typedef SSIZE_T ssize_t;
36 #endif /* _MSC_VER */
37
38 #include <limits.h>
39 #include <stdint.h>
40 #include <sys/types.h>
41 #if !defined(_MSC_VER)
42 #include <sys/time.h>
43 #endif
44 #include <time.h>
45
46 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
47 #define ZERO_SIZED_ARRAY                /* [] - valid C99 code */
48 #else
49 #define ZERO_SIZED_ARRAY        0       /* [0] - non-standard, but usually working code */
50 #endif /* __STDC_VERSION__ */
51
52 /* 'interface' might be defined as a macro on Windows, so we need to
53  * undefine it so as not to break the current libusb API, because
54  * libusb_config_descriptor has an 'interface' member
55  * As this can be problematic if you include windows.h after libusb.h
56  * in your sources, we force windows.h to be included first. */
57 #if defined(_WIN32) || defined(__CYGWIN__)
58 #include <windows.h>
59 #if defined(interface)
60 #undef interface
61 #endif
62 #if !defined(__CYGWIN__)
63 #include <winsock.h>
64 #endif
65 #endif /* _WIN32 || __CYGWIN__ */
66
67 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
68 #define LIBUSB_DEPRECATED_FOR(f) __attribute__ ((deprecated ("Use " #f " instead")))
69 #elif defined(__GNUC__) && (__GNUC__ >= 3)
70 #define LIBUSB_DEPRECATED_FOR(f) __attribute__ ((deprecated))
71 #else
72 #define LIBUSB_DEPRECATED_FOR(f)
73 #endif /* __GNUC__ */
74
75 #if defined(__GNUC__)
76 #define LIBUSB_PACKED __attribute__ ((packed))
77 #else
78 #define LIBUSB_PACKED
79 #endif /* __GNUC__ */
80
81 /** \def LIBUSB_CALL
82  * \ingroup libusb_misc
83  * libusb's Windows calling convention.
84  *
85  * Under Windows, the selection of available compilers and configurations
86  * means that, unlike other platforms, there is not <em>one true calling
87  * convention</em> (calling convention: the manner in which parameters are
88  * passed to functions in the generated assembly code).
89  *
90  * Matching the Windows API itself, libusb uses the WINAPI convention (which
91  * translates to the <tt>stdcall</tt> convention) and guarantees that the
92  * library is compiled in this way. The public header file also includes
93  * appropriate annotations so that your own software will use the right
94  * convention, even if another convention is being used by default within
95  * your codebase.
96  *
97  * The one consideration that you must apply in your software is to mark
98  * all functions which you use as libusb callbacks with this LIBUSB_CALL
99  * annotation, so that they too get compiled for the correct calling
100  * convention.
101  *
102  * On non-Windows operating systems, this macro is defined as nothing. This
103  * means that you can apply it to your code without worrying about
104  * cross-platform compatibility.
105  */
106 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
107  * functions. You'd think that declaration would be enough, but cygwin will
108  * complain about conflicting types unless both are marked this way.
109  * The placement of this macro is important too; it must appear after the
110  * return type, before the function name. See internal documentation for
111  * API_EXPORTED.
112  */
113 #if defined(_WIN32) || defined(__CYGWIN__)
114 #define LIBUSB_CALL WINAPI
115 #else
116 #define LIBUSB_CALL
117 #endif /* _WIN32 || __CYGWIN__ */
118
119 /** \def LIBUSB_API_VERSION
120  * \ingroup libusb_misc
121  * libusb's API version.
122  *
123  * Since version 1.0.13, to help with feature detection, libusb defines
124  * a LIBUSB_API_VERSION macro that gets increased every time there is a
125  * significant change to the API, such as the introduction of a new call,
126  * the definition of a new macro/enum member, or any other element that
127  * libusb applications may want to detect at compilation time.
128  *
129  * The macro is typically used in an application as follows:
130  * \code
131  * #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234)
132  * // Use one of the newer features from the libusb API
133  * #endif
134  * \endcode
135  *
136  * Internally, LIBUSB_API_VERSION is defined as follows:
137  * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental)
138  */
139 #define LIBUSB_API_VERSION 0x01000108
140
141 /* The following is kept for compatibility, but will be deprecated in the future */
142 #define LIBUSBX_API_VERSION LIBUSB_API_VERSION
143
144 #if defined(__cplusplus)
145 extern "C" {
146 #endif
147
148 /**
149  * \ingroup libusb_misc
150  * Convert a 16-bit value from host-endian to little-endian format. On
151  * little endian systems, this function does nothing. On big endian systems,
152  * the bytes are swapped.
153  * \param x the host-endian value to convert
154  * \returns the value in little-endian byte order
155  */
156 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
157 {
158         union {
159                 uint8_t  b8[2];
160                 uint16_t b16;
161         } _tmp;
162         _tmp.b8[1] = (uint8_t) (x >> 8);
163         _tmp.b8[0] = (uint8_t) (x & 0xff);
164         return _tmp.b16;
165 }
166
167 /** \def libusb_le16_to_cpu
168  * \ingroup libusb_misc
169  * Convert a 16-bit value from little-endian to host-endian format. On
170  * little endian systems, this function does nothing. On big endian systems,
171  * the bytes are swapped.
172  * \param x the little-endian value to convert
173  * \returns the value in host-endian byte order
174  */
175 #define libusb_le16_to_cpu libusb_cpu_to_le16
176
177 /* standard USB stuff */
178
179 /** \ingroup libusb_desc
180  * Device and/or Interface Class codes */
181 enum libusb_class_code {
182         /** In the context of a \ref libusb_device_descriptor "device descriptor",
183          * this bDeviceClass value indicates that each interface specifies its
184          * own class information and all interfaces operate independently.
185          */
186         LIBUSB_CLASS_PER_INTERFACE = 0x00,
187
188         /** Audio class */
189         LIBUSB_CLASS_AUDIO = 0x01,
190
191         /** Communications class */
192         LIBUSB_CLASS_COMM = 0x02,
193
194         /** Human Interface Device class */
195         LIBUSB_CLASS_HID = 0x03,
196
197         /** Physical */
198         LIBUSB_CLASS_PHYSICAL = 0x05,
199
200         /** Image class */
201         LIBUSB_CLASS_IMAGE = 0x06,
202         LIBUSB_CLASS_PTP = 0x06, /* legacy name from libusb-0.1 usb.h */
203
204         /** Printer class */
205         LIBUSB_CLASS_PRINTER = 0x07,
206
207         /** Mass storage class */
208         LIBUSB_CLASS_MASS_STORAGE = 0x08,
209
210         /** Hub class */
211         LIBUSB_CLASS_HUB = 0x09,
212
213         /** Data class */
214         LIBUSB_CLASS_DATA = 0x0a,
215
216         /** Smart Card */
217         LIBUSB_CLASS_SMART_CARD = 0x0b,
218
219         /** Content Security */
220         LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
221
222         /** Video */
223         LIBUSB_CLASS_VIDEO = 0x0e,
224
225         /** Personal Healthcare */
226         LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
227
228         /** Diagnostic Device */
229         LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
230
231         /** Wireless class */
232         LIBUSB_CLASS_WIRELESS = 0xe0,
233
234         /** Miscellaneous class */
235         LIBUSB_CLASS_MISCELLANEOUS = 0xef,
236
237         /** Application class */
238         LIBUSB_CLASS_APPLICATION = 0xfe,
239
240         /** Class is vendor-specific */
241         LIBUSB_CLASS_VENDOR_SPEC = 0xff
242 };
243
244 /** \ingroup libusb_desc
245  * Descriptor types as defined by the USB specification. */
246 enum libusb_descriptor_type {
247         /** Device descriptor. See libusb_device_descriptor. */
248         LIBUSB_DT_DEVICE = 0x01,
249
250         /** Configuration descriptor. See libusb_config_descriptor. */
251         LIBUSB_DT_CONFIG = 0x02,
252
253         /** String descriptor */
254         LIBUSB_DT_STRING = 0x03,
255
256         /** Interface descriptor. See libusb_interface_descriptor. */
257         LIBUSB_DT_INTERFACE = 0x04,
258
259         /** Endpoint descriptor. See libusb_endpoint_descriptor. */
260         LIBUSB_DT_ENDPOINT = 0x05,
261
262         /** BOS descriptor */
263         LIBUSB_DT_BOS = 0x0f,
264
265         /** Device Capability descriptor */
266         LIBUSB_DT_DEVICE_CAPABILITY = 0x10,
267
268         /** HID descriptor */
269         LIBUSB_DT_HID = 0x21,
270
271         /** HID report descriptor */
272         LIBUSB_DT_REPORT = 0x22,
273
274         /** Physical descriptor */
275         LIBUSB_DT_PHYSICAL = 0x23,
276
277         /** Hub descriptor */
278         LIBUSB_DT_HUB = 0x29,
279
280         /** SuperSpeed Hub descriptor */
281         LIBUSB_DT_SUPERSPEED_HUB = 0x2a,
282
283         /** SuperSpeed Endpoint Companion descriptor */
284         LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30
285 };
286
287 /* Descriptor sizes per descriptor type */
288 #define LIBUSB_DT_DEVICE_SIZE                   18
289 #define LIBUSB_DT_CONFIG_SIZE                   9
290 #define LIBUSB_DT_INTERFACE_SIZE                9
291 #define LIBUSB_DT_ENDPOINT_SIZE                 7
292 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE           9       /* Audio extension */
293 #define LIBUSB_DT_HUB_NONVAR_SIZE               7
294 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE    6
295 #define LIBUSB_DT_BOS_SIZE                      5
296 #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE        3
297
298 /* BOS descriptor sizes */
299 #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE        7
300 #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10
301 #define LIBUSB_BT_CONTAINER_ID_SIZE             20
302
303 /* We unwrap the BOS => define its max size */
304 #define LIBUSB_DT_BOS_MAX_SIZE                          \
305         (LIBUSB_DT_BOS_SIZE +                           \
306          LIBUSB_BT_USB_2_0_EXTENSION_SIZE +             \
307          LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE +      \
308          LIBUSB_BT_CONTAINER_ID_SIZE)
309
310 #define LIBUSB_ENDPOINT_ADDRESS_MASK            0x0f    /* in bEndpointAddress */
311 #define LIBUSB_ENDPOINT_DIR_MASK                0x80
312
313 /** \ingroup libusb_desc
314  * Endpoint direction. Values for bit 7 of the
315  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
316  */
317 enum libusb_endpoint_direction {
318         /** Out: host-to-device */
319         LIBUSB_ENDPOINT_OUT = 0x00,
320
321         /** In: device-to-host */
322         LIBUSB_ENDPOINT_IN = 0x80
323 };
324
325 #define LIBUSB_TRANSFER_TYPE_MASK               0x03    /* in bmAttributes */
326
327 /** \ingroup libusb_desc
328  * Endpoint transfer type. Values for bits 0:1 of the
329  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
330  */
331 enum libusb_endpoint_transfer_type {
332         /** Control endpoint */
333         LIBUSB_ENDPOINT_TRANSFER_TYPE_CONTROL = 0x0,
334
335         /** Isochronous endpoint */
336         LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS = 0x1,
337
338         /** Bulk endpoint */
339         LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK = 0x2,
340
341         /** Interrupt endpoint */
342         LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT = 0x3
343 };
344
345 /** \ingroup libusb_misc
346  * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
347 enum libusb_standard_request {
348         /** Request status of the specific recipient */
349         LIBUSB_REQUEST_GET_STATUS = 0x00,
350
351         /** Clear or disable a specific feature */
352         LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
353
354         /* 0x02 is reserved */
355
356         /** Set or enable a specific feature */
357         LIBUSB_REQUEST_SET_FEATURE = 0x03,
358
359         /* 0x04 is reserved */
360
361         /** Set device address for all future accesses */
362         LIBUSB_REQUEST_SET_ADDRESS = 0x05,
363
364         /** Get the specified descriptor */
365         LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
366
367         /** Used to update existing descriptors or add new descriptors */
368         LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
369
370         /** Get the current device configuration value */
371         LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
372
373         /** Set device configuration */
374         LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
375
376         /** Return the selected alternate setting for the specified interface */
377         LIBUSB_REQUEST_GET_INTERFACE = 0x0a,
378
379         /** Select an alternate interface for the specified interface */
380         LIBUSB_REQUEST_SET_INTERFACE = 0x0b,
381
382         /** Set then report an endpoint's synchronization frame */
383         LIBUSB_REQUEST_SYNCH_FRAME = 0x0c,
384
385         /** Sets both the U1 and U2 Exit Latency */
386         LIBUSB_REQUEST_SET_SEL = 0x30,
387
388         /** Delay from the time a host transmits a packet to the time it is
389           * received by the device. */
390         LIBUSB_SET_ISOCH_DELAY = 0x31
391 };
392
393 /** \ingroup libusb_misc
394  * Request type bits of the
395  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
396  * transfers. */
397 enum libusb_request_type {
398         /** Standard */
399         LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
400
401         /** Class */
402         LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
403
404         /** Vendor */
405         LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
406
407         /** Reserved */
408         LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
409 };
410
411 /** \ingroup libusb_misc
412  * Recipient bits of the
413  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
414  * transfers. Values 4 through 31 are reserved. */
415 enum libusb_request_recipient {
416         /** Device */
417         LIBUSB_RECIPIENT_DEVICE = 0x00,
418
419         /** Interface */
420         LIBUSB_RECIPIENT_INTERFACE = 0x01,
421
422         /** Endpoint */
423         LIBUSB_RECIPIENT_ENDPOINT = 0x02,
424
425         /** Other */
426         LIBUSB_RECIPIENT_OTHER = 0x03
427 };
428
429 #define LIBUSB_ISO_SYNC_TYPE_MASK       0x0c
430
431 /** \ingroup libusb_desc
432  * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
433  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
434  * libusb_endpoint_descriptor.
435  */
436 enum libusb_iso_sync_type {
437         /** No synchronization */
438         LIBUSB_ISO_SYNC_TYPE_NONE = 0x0,
439
440         /** Asynchronous */
441         LIBUSB_ISO_SYNC_TYPE_ASYNC = 0x1,
442
443         /** Adaptive */
444         LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 0x2,
445
446         /** Synchronous */
447         LIBUSB_ISO_SYNC_TYPE_SYNC = 0x3
448 };
449
450 #define LIBUSB_ISO_USAGE_TYPE_MASK      0x30
451
452 /** \ingroup libusb_desc
453  * Usage type for isochronous endpoints. Values for bits 4:5 of the
454  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
455  * libusb_endpoint_descriptor.
456  */
457 enum libusb_iso_usage_type {
458         /** Data endpoint */
459         LIBUSB_ISO_USAGE_TYPE_DATA = 0x0,
460
461         /** Feedback endpoint */
462         LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 0x1,
463
464         /** Implicit feedback Data endpoint */
465         LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 0x2
466 };
467
468 /** \ingroup libusb_desc
469  * Supported speeds (wSpeedSupported) bitfield. Indicates what
470  * speeds the device supports.
471  */
472 enum libusb_supported_speed {
473         /** Low speed operation supported (1.5MBit/s). */
474         LIBUSB_LOW_SPEED_OPERATION = (1 << 0),
475
476         /** Full speed operation supported (12MBit/s). */
477         LIBUSB_FULL_SPEED_OPERATION = (1 << 1),
478
479         /** High speed operation supported (480MBit/s). */
480         LIBUSB_HIGH_SPEED_OPERATION = (1 << 2),
481
482         /** Superspeed operation supported (5000MBit/s). */
483         LIBUSB_SUPER_SPEED_OPERATION = (1 << 3)
484 };
485
486 /** \ingroup libusb_desc
487  * Masks for the bits of the
488  * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field
489  * of the USB 2.0 Extension descriptor.
490  */
491 enum libusb_usb_2_0_extension_attributes {
492         /** Supports Link Power Management (LPM) */
493         LIBUSB_BM_LPM_SUPPORT = (1 << 1)
494 };
495
496 /** \ingroup libusb_desc
497  * Masks for the bits of the
498  * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field
499  * field of the SuperSpeed USB Device Capability descriptor.
500  */
501 enum libusb_ss_usb_device_capability_attributes {
502         /** Supports Latency Tolerance Messages (LTM) */
503         LIBUSB_BM_LTM_SUPPORT = (1 << 1)
504 };
505
506 /** \ingroup libusb_desc
507  * USB capability types
508  */
509 enum libusb_bos_type {
510         /** Wireless USB device capability */
511         LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 0x01,
512
513         /** USB 2.0 extensions */
514         LIBUSB_BT_USB_2_0_EXTENSION = 0x02,
515
516         /** SuperSpeed USB device capability */
517         LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 0x03,
518
519         /** Container ID type */
520         LIBUSB_BT_CONTAINER_ID = 0x04
521 };
522
523 /** \ingroup libusb_desc
524  * A structure representing the standard USB device descriptor. This
525  * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
526  * All multiple-byte fields are represented in host-endian format.
527  */
528 struct libusb_device_descriptor {
529         /** Size of this descriptor (in bytes) */
530         uint8_t  bLength;
531
532         /** Descriptor type. Will have value
533          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
534          * context. */
535         uint8_t  bDescriptorType;
536
537         /** USB specification release number in binary-coded decimal. A value of
538          * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
539         uint16_t bcdUSB;
540
541         /** USB-IF class code for the device. See \ref libusb_class_code. */
542         uint8_t  bDeviceClass;
543
544         /** USB-IF subclass code for the device, qualified by the bDeviceClass
545          * value */
546         uint8_t  bDeviceSubClass;
547
548         /** USB-IF protocol code for the device, qualified by the bDeviceClass and
549          * bDeviceSubClass values */
550         uint8_t  bDeviceProtocol;
551
552         /** Maximum packet size for endpoint 0 */
553         uint8_t  bMaxPacketSize0;
554
555         /** USB-IF vendor ID */
556         uint16_t idVendor;
557
558         /** USB-IF product ID */
559         uint16_t idProduct;
560
561         /** Device release number in binary-coded decimal */
562         uint16_t bcdDevice;
563
564         /** Index of string descriptor describing manufacturer */
565         uint8_t  iManufacturer;
566
567         /** Index of string descriptor describing product */
568         uint8_t  iProduct;
569
570         /** Index of string descriptor containing device serial number */
571         uint8_t  iSerialNumber;
572
573         /** Number of possible configurations */
574         uint8_t  bNumConfigurations;
575 };
576
577 /** \ingroup libusb_desc
578  * A structure representing the standard USB endpoint descriptor. This
579  * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
580  * All multiple-byte fields are represented in host-endian format.
581  */
582 struct libusb_endpoint_descriptor {
583         /** Size of this descriptor (in bytes) */
584         uint8_t  bLength;
585
586         /** Descriptor type. Will have value
587          * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
588          * this context. */
589         uint8_t  bDescriptorType;
590
591         /** The address of the endpoint described by this descriptor. Bits 0:3 are
592          * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
593          * see \ref libusb_endpoint_direction. */
594         uint8_t  bEndpointAddress;
595
596         /** Attributes which apply to the endpoint when it is configured using
597          * the bConfigurationValue. Bits 0:1 determine the transfer type and
598          * correspond to \ref libusb_endpoint_transfer_type. Bits 2:3 are only used
599          * for isochronous endpoints and correspond to \ref libusb_iso_sync_type.
600          * Bits 4:5 are also only used for isochronous endpoints and correspond to
601          * \ref libusb_iso_usage_type. Bits 6:7 are reserved. */
602         uint8_t  bmAttributes;
603
604         /** Maximum packet size this endpoint is capable of sending/receiving. */
605         uint16_t wMaxPacketSize;
606
607         /** Interval for polling endpoint for data transfers. */
608         uint8_t  bInterval;
609
610         /** For audio devices only: the rate at which synchronization feedback
611          * is provided. */
612         uint8_t  bRefresh;
613
614         /** For audio devices only: the address if the synch endpoint */
615         uint8_t  bSynchAddress;
616
617         /** Extra descriptors. If libusb encounters unknown endpoint descriptors,
618          * it will store them here, should you wish to parse them. */
619         const unsigned char *extra;
620
621         /** Length of the extra descriptors, in bytes. Must be non-negative. */
622         int extra_length;
623 };
624
625 /** \ingroup libusb_desc
626  * A structure representing the standard USB interface descriptor. This
627  * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
628  * All multiple-byte fields are represented in host-endian format.
629  */
630 struct libusb_interface_descriptor {
631         /** Size of this descriptor (in bytes) */
632         uint8_t  bLength;
633
634         /** Descriptor type. Will have value
635          * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
636          * in this context. */
637         uint8_t  bDescriptorType;
638
639         /** Number of this interface */
640         uint8_t  bInterfaceNumber;
641
642         /** Value used to select this alternate setting for this interface */
643         uint8_t  bAlternateSetting;
644
645         /** Number of endpoints used by this interface (excluding the control
646          * endpoint). */
647         uint8_t  bNumEndpoints;
648
649         /** USB-IF class code for this interface. See \ref libusb_class_code. */
650         uint8_t  bInterfaceClass;
651
652         /** USB-IF subclass code for this interface, qualified by the
653          * bInterfaceClass value */
654         uint8_t  bInterfaceSubClass;
655
656         /** USB-IF protocol code for this interface, qualified by the
657          * bInterfaceClass and bInterfaceSubClass values */
658         uint8_t  bInterfaceProtocol;
659
660         /** Index of string descriptor describing this interface */
661         uint8_t  iInterface;
662
663         /** Array of endpoint descriptors. This length of this array is determined
664          * by the bNumEndpoints field. */
665         const struct libusb_endpoint_descriptor *endpoint;
666
667         /** Extra descriptors. If libusb encounters unknown interface descriptors,
668          * it will store them here, should you wish to parse them. */
669         const unsigned char *extra;
670
671         /** Length of the extra descriptors, in bytes. Must be non-negative. */
672         int extra_length;
673 };
674
675 /** \ingroup libusb_desc
676  * A collection of alternate settings for a particular USB interface.
677  */
678 struct libusb_interface {
679         /** Array of interface descriptors. The length of this array is determined
680          * by the num_altsetting field. */
681         const struct libusb_interface_descriptor *altsetting;
682
683         /** The number of alternate settings that belong to this interface.
684          * Must be non-negative. */
685         int num_altsetting;
686 };
687
688 /** \ingroup libusb_desc
689  * A structure representing the standard USB configuration descriptor. This
690  * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
691  * All multiple-byte fields are represented in host-endian format.
692  */
693 struct libusb_config_descriptor {
694         /** Size of this descriptor (in bytes) */
695         uint8_t  bLength;
696
697         /** Descriptor type. Will have value
698          * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
699          * in this context. */
700         uint8_t  bDescriptorType;
701
702         /** Total length of data returned for this configuration */
703         uint16_t wTotalLength;
704
705         /** Number of interfaces supported by this configuration */
706         uint8_t  bNumInterfaces;
707
708         /** Identifier value for this configuration */
709         uint8_t  bConfigurationValue;
710
711         /** Index of string descriptor describing this configuration */
712         uint8_t  iConfiguration;
713
714         /** Configuration characteristics */
715         uint8_t  bmAttributes;
716
717         /** Maximum power consumption of the USB device from this bus in this
718          * configuration when the device is fully operation. Expressed in units
719          * of 2 mA when the device is operating in high-speed mode and in units
720          * of 8 mA when the device is operating in super-speed mode. */
721         uint8_t  MaxPower;
722
723         /** Array of interfaces supported by this configuration. The length of
724          * this array is determined by the bNumInterfaces field. */
725         const struct libusb_interface *interface;
726
727         /** Extra descriptors. If libusb encounters unknown configuration
728          * descriptors, it will store them here, should you wish to parse them. */
729         const unsigned char *extra;
730
731         /** Length of the extra descriptors, in bytes. Must be non-negative. */
732         int extra_length;
733 };
734
735 /** \ingroup libusb_desc
736  * A structure representing the superspeed endpoint companion
737  * descriptor. This descriptor is documented in section 9.6.7 of
738  * the USB 3.0 specification. All multiple-byte fields are represented in
739  * host-endian format.
740  */
741 struct libusb_ss_endpoint_companion_descriptor {
742         /** Size of this descriptor (in bytes) */
743         uint8_t  bLength;
744
745         /** Descriptor type. Will have value
746          * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in
747          * this context. */
748         uint8_t  bDescriptorType;
749
750         /** The maximum number of packets the endpoint can send or
751          *  receive as part of a burst. */
752         uint8_t  bMaxBurst;
753
754         /** In bulk EP: bits 4:0 represents the maximum number of
755          *  streams the EP supports. In isochronous EP: bits 1:0
756          *  represents the Mult - a zero based value that determines
757          *  the maximum number of packets within a service interval  */
758         uint8_t  bmAttributes;
759
760         /** The total number of bytes this EP will transfer every
761          *  service interval. Valid only for periodic EPs. */
762         uint16_t wBytesPerInterval;
763 };
764
765 /** \ingroup libusb_desc
766  * A generic representation of a BOS Device Capability descriptor. It is
767  * advised to check bDevCapabilityType and call the matching
768  * libusb_get_*_descriptor function to get a structure fully matching the type.
769  */
770 struct libusb_bos_dev_capability_descriptor {
771         /** Size of this descriptor (in bytes) */
772         uint8_t  bLength;
773
774         /** Descriptor type. Will have value
775          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
776          * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
777         uint8_t  bDescriptorType;
778
779         /** Device Capability type */
780         uint8_t  bDevCapabilityType;
781
782         /** Device Capability data (bLength - 3 bytes) */
783         uint8_t  dev_capability_data[ZERO_SIZED_ARRAY];
784 };
785
786 /** \ingroup libusb_desc
787  * A structure representing the Binary Device Object Store (BOS) descriptor.
788  * This descriptor is documented in section 9.6.2 of the USB 3.0 specification.
789  * All multiple-byte fields are represented in host-endian format.
790  */
791 struct libusb_bos_descriptor {
792         /** Size of this descriptor (in bytes) */
793         uint8_t  bLength;
794
795         /** Descriptor type. Will have value
796          * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS
797          * in this context. */
798         uint8_t  bDescriptorType;
799
800         /** Length of this descriptor and all of its sub descriptors */
801         uint16_t wTotalLength;
802
803         /** The number of separate device capability descriptors in
804          * the BOS */
805         uint8_t  bNumDeviceCaps;
806
807         /** bNumDeviceCap Device Capability Descriptors */
808         struct libusb_bos_dev_capability_descriptor *dev_capability[ZERO_SIZED_ARRAY];
809 };
810
811 /** \ingroup libusb_desc
812  * A structure representing the USB 2.0 Extension descriptor
813  * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification.
814  * All multiple-byte fields are represented in host-endian format.
815  */
816 struct libusb_usb_2_0_extension_descriptor {
817         /** Size of this descriptor (in bytes) */
818         uint8_t  bLength;
819
820         /** Descriptor type. Will have value
821          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
822          * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
823         uint8_t  bDescriptorType;
824
825         /** Capability type. Will have value
826          * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
827          * LIBUSB_BT_USB_2_0_EXTENSION in this context. */
828         uint8_t  bDevCapabilityType;
829
830         /** Bitmap encoding of supported device level features.
831          * A value of one in a bit location indicates a feature is
832          * supported; a value of zero indicates it is not supported.
833          * See \ref libusb_usb_2_0_extension_attributes. */
834         uint32_t bmAttributes;
835 };
836
837 /** \ingroup libusb_desc
838  * A structure representing the SuperSpeed USB Device Capability descriptor
839  * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification.
840  * All multiple-byte fields are represented in host-endian format.
841  */
842 struct libusb_ss_usb_device_capability_descriptor {
843         /** Size of this descriptor (in bytes) */
844         uint8_t  bLength;
845
846         /** Descriptor type. Will have value
847          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
848          * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
849         uint8_t  bDescriptorType;
850
851         /** Capability type. Will have value
852          * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
853          * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */
854         uint8_t  bDevCapabilityType;
855
856         /** Bitmap encoding of supported device level features.
857          * A value of one in a bit location indicates a feature is
858          * supported; a value of zero indicates it is not supported.
859          * See \ref libusb_ss_usb_device_capability_attributes. */
860         uint8_t  bmAttributes;
861
862         /** Bitmap encoding of the speed supported by this device when
863          * operating in SuperSpeed mode. See \ref libusb_supported_speed. */
864         uint16_t wSpeedSupported;
865
866         /** The lowest speed at which all the functionality supported
867          * by the device is available to the user. For example if the
868          * device supports all its functionality when connected at
869          * full speed and above then it sets this value to 1. */
870         uint8_t  bFunctionalitySupport;
871
872         /** U1 Device Exit Latency. */
873         uint8_t  bU1DevExitLat;
874
875         /** U2 Device Exit Latency. */
876         uint16_t bU2DevExitLat;
877 };
878
879 /** \ingroup libusb_desc
880  * A structure representing the Container ID descriptor.
881  * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification.
882  * All multiple-byte fields, except UUIDs, are represented in host-endian format.
883  */
884 struct libusb_container_id_descriptor {
885         /** Size of this descriptor (in bytes) */
886         uint8_t  bLength;
887
888         /** Descriptor type. Will have value
889          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
890          * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
891         uint8_t  bDescriptorType;
892
893         /** Capability type. Will have value
894          * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
895          * LIBUSB_BT_CONTAINER_ID in this context. */
896         uint8_t  bDevCapabilityType;
897
898         /** Reserved field */
899         uint8_t  bReserved;
900
901         /** 128 bit UUID */
902         uint8_t  ContainerID[16];
903 };
904
905 /** \ingroup libusb_asyncio
906  * Setup packet for control transfers. */
907 #if defined(_MSC_VER)
908 #pragma pack(push, 1)
909 #endif
910 struct libusb_control_setup {
911         /** Request type. Bits 0:4 determine recipient, see
912          * \ref libusb_request_recipient. Bits 5:6 determine type, see
913          * \ref libusb_request_type. Bit 7 determines data transfer direction, see
914          * \ref libusb_endpoint_direction.
915          */
916         uint8_t  bmRequestType;
917
918         /** Request. If the type bits of bmRequestType are equal to
919          * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
920          * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
921          * \ref libusb_standard_request. For other cases, use of this field is
922          * application-specific. */
923         uint8_t  bRequest;
924
925         /** Value. Varies according to request */
926         uint16_t wValue;
927
928         /** Index. Varies according to request, typically used to pass an index
929          * or offset */
930         uint16_t wIndex;
931
932         /** Number of bytes to transfer */
933         uint16_t wLength;
934 } LIBUSB_PACKED;
935 #if defined(_MSC_VER)
936 #pragma pack(pop)
937 #endif
938
939 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
940
941 /* libusb */
942
943 struct libusb_context;
944 struct libusb_device;
945 struct libusb_device_handle;
946
947 /** \ingroup libusb_lib
948  * Structure providing the version of the libusb runtime
949  */
950 struct libusb_version {
951         /** Library major version. */
952         const uint16_t major;
953
954         /** Library minor version. */
955         const uint16_t minor;
956
957         /** Library micro version. */
958         const uint16_t micro;
959
960         /** Library nano version. */
961         const uint16_t nano;
962
963         /** Library release candidate suffix string, e.g. "-rc4". */
964         const char *rc;
965
966         /** For ABI compatibility only. */
967         const char *describe;
968 };
969
970 /** \ingroup libusb_lib
971  * Structure representing a libusb session. The concept of individual libusb
972  * sessions allows for your program to use two libraries (or dynamically
973  * load two modules) which both independently use libusb. This will prevent
974  * interference between the individual libusb users - for example
975  * libusb_set_option() will not affect the other user of the library, and
976  * libusb_exit() will not destroy resources that the other user is still
977  * using.
978  *
979  * Sessions are created by libusb_init() and destroyed through libusb_exit().
980  * If your application is guaranteed to only ever include a single libusb
981  * user (i.e. you), you do not have to worry about contexts: pass NULL in
982  * every function call where a context is required. The default context
983  * will be used.
984  *
985  * For more information, see \ref libusb_contexts.
986  */
987 typedef struct libusb_context libusb_context;
988
989 /** \ingroup libusb_dev
990  * Structure representing a USB device detected on the system. This is an
991  * opaque type for which you are only ever provided with a pointer, usually
992  * originating from libusb_get_device_list().
993  *
994  * Certain operations can be performed on a device, but in order to do any
995  * I/O you will have to first obtain a device handle using libusb_open().
996  *
997  * Devices are reference counted with libusb_ref_device() and
998  * libusb_unref_device(), and are freed when the reference count reaches 0.
999  * New devices presented by libusb_get_device_list() have a reference count of
1000  * 1, and libusb_free_device_list() can optionally decrease the reference count
1001  * on all devices in the list. libusb_open() adds another reference which is
1002  * later destroyed by libusb_close().
1003  */
1004 typedef struct libusb_device libusb_device;
1005
1006
1007 /** \ingroup libusb_dev
1008  * Structure representing a handle on a USB device. This is an opaque type for
1009  * which you are only ever provided with a pointer, usually originating from
1010  * libusb_open().
1011  *
1012  * A device handle is used to perform I/O and other operations. When finished
1013  * with a device handle, you should call libusb_close().
1014  */
1015 typedef struct libusb_device_handle libusb_device_handle;
1016
1017 /** \ingroup libusb_dev
1018  * Speed codes. Indicates the speed at which the device is operating.
1019  */
1020 enum libusb_speed {
1021         /** The OS doesn't report or know the device speed. */
1022         LIBUSB_SPEED_UNKNOWN = 0,
1023
1024         /** The device is operating at low speed (1.5MBit/s). */
1025         LIBUSB_SPEED_LOW = 1,
1026
1027         /** The device is operating at full speed (12MBit/s). */
1028         LIBUSB_SPEED_FULL = 2,
1029
1030         /** The device is operating at high speed (480MBit/s). */
1031         LIBUSB_SPEED_HIGH = 3,
1032
1033         /** The device is operating at super speed (5000MBit/s). */
1034         LIBUSB_SPEED_SUPER = 4,
1035
1036         /** The device is operating at super speed plus (10000MBit/s). */
1037         LIBUSB_SPEED_SUPER_PLUS = 5
1038 };
1039
1040 /** \ingroup libusb_misc
1041  * Error codes. Most libusb functions return 0 on success or one of these
1042  * codes on failure.
1043  * You can call libusb_error_name() to retrieve a string representation of an
1044  * error code or libusb_strerror() to get an end-user suitable description of
1045  * an error code.
1046  */
1047 enum libusb_error {
1048         /** Success (no error) */
1049         LIBUSB_SUCCESS = 0,
1050
1051         /** Input/output error */
1052         LIBUSB_ERROR_IO = -1,
1053
1054         /** Invalid parameter */
1055         LIBUSB_ERROR_INVALID_PARAM = -2,
1056
1057         /** Access denied (insufficient permissions) */
1058         LIBUSB_ERROR_ACCESS = -3,
1059
1060         /** No such device (it may have been disconnected) */
1061         LIBUSB_ERROR_NO_DEVICE = -4,
1062
1063         /** Entity not found */
1064         LIBUSB_ERROR_NOT_FOUND = -5,
1065
1066         /** Resource busy */
1067         LIBUSB_ERROR_BUSY = -6,
1068
1069         /** Operation timed out */
1070         LIBUSB_ERROR_TIMEOUT = -7,
1071
1072         /** Overflow */
1073         LIBUSB_ERROR_OVERFLOW = -8,
1074
1075         /** Pipe error */
1076         LIBUSB_ERROR_PIPE = -9,
1077
1078         /** System call interrupted (perhaps due to signal) */
1079         LIBUSB_ERROR_INTERRUPTED = -10,
1080
1081         /** Insufficient memory */
1082         LIBUSB_ERROR_NO_MEM = -11,
1083
1084         /** Operation not supported or unimplemented on this platform */
1085         LIBUSB_ERROR_NOT_SUPPORTED = -12,
1086
1087         /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the
1088            message strings in strerror.c when adding new error codes here. */
1089
1090         /** Other error */
1091         LIBUSB_ERROR_OTHER = -99
1092 };
1093
1094 /* Total number of error codes in enum libusb_error */
1095 #define LIBUSB_ERROR_COUNT 14
1096
1097 /** \ingroup libusb_asyncio
1098  * Transfer type */
1099 enum libusb_transfer_type {
1100         /** Control transfer */
1101         LIBUSB_TRANSFER_TYPE_CONTROL = 0U,
1102
1103         /** Isochronous transfer */
1104         LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1U,
1105
1106         /** Bulk transfer */
1107         LIBUSB_TRANSFER_TYPE_BULK = 2U,
1108
1109         /** Interrupt transfer */
1110         LIBUSB_TRANSFER_TYPE_INTERRUPT = 3U,
1111
1112         /** Bulk stream transfer */
1113         LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4U
1114 };
1115
1116 /** \ingroup libusb_asyncio
1117  * Transfer status codes */
1118 enum libusb_transfer_status {
1119         /** Transfer completed without error. Note that this does not indicate
1120          * that the entire amount of requested data was transferred. */
1121         LIBUSB_TRANSFER_COMPLETED,
1122
1123         /** Transfer failed */
1124         LIBUSB_TRANSFER_ERROR,
1125
1126         /** Transfer timed out */
1127         LIBUSB_TRANSFER_TIMED_OUT,
1128
1129         /** Transfer was cancelled */
1130         LIBUSB_TRANSFER_CANCELLED,
1131
1132         /** For bulk/interrupt endpoints: halt condition detected (endpoint
1133          * stalled). For control endpoints: control request not supported. */
1134         LIBUSB_TRANSFER_STALL,
1135
1136         /** Device was disconnected */
1137         LIBUSB_TRANSFER_NO_DEVICE,
1138
1139         /** Device sent more data than requested */
1140         LIBUSB_TRANSFER_OVERFLOW
1141
1142         /* NB! Remember to update libusb_error_name()
1143            when adding new status codes here. */
1144 };
1145
1146 /** \ingroup libusb_asyncio
1147  * libusb_transfer.flags values */
1148 enum libusb_transfer_flags {
1149         /** Report short frames as errors */
1150         LIBUSB_TRANSFER_SHORT_NOT_OK = (1U << 0),
1151
1152         /** Automatically free() transfer buffer during libusb_free_transfer().
1153          * Note that buffers allocated with libusb_dev_mem_alloc() should not
1154          * be attempted freed in this way, since free() is not an appropriate
1155          * way to release such memory. */
1156         LIBUSB_TRANSFER_FREE_BUFFER = (1U << 1),
1157
1158         /** Automatically call libusb_free_transfer() after callback returns.
1159          * If this flag is set, it is illegal to call libusb_free_transfer()
1160          * from your transfer callback, as this will result in a double-free
1161          * when this flag is acted upon. */
1162         LIBUSB_TRANSFER_FREE_TRANSFER = (1U << 2),
1163
1164         /** Terminate transfers that are a multiple of the endpoint's
1165          * wMaxPacketSize with an extra zero length packet. This is useful
1166          * when a device protocol mandates that each logical request is
1167          * terminated by an incomplete packet (i.e. the logical requests are
1168          * not separated by other means).
1169          *
1170          * This flag only affects host-to-device transfers to bulk and interrupt
1171          * endpoints. In other situations, it is ignored.
1172          *
1173          * This flag only affects transfers with a length that is a multiple of
1174          * the endpoint's wMaxPacketSize. On transfers of other lengths, this
1175          * flag has no effect. Therefore, if you are working with a device that
1176          * needs a ZLP whenever the end of the logical request falls on a packet
1177          * boundary, then it is sensible to set this flag on <em>every</em>
1178          * transfer (you do not have to worry about only setting it on transfers
1179          * that end on the boundary).
1180          *
1181          * This flag is currently only supported on Linux.
1182          * On other systems, libusb_submit_transfer() will return
1183          * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
1184          *
1185          * Available since libusb-1.0.9.
1186          */
1187         LIBUSB_TRANSFER_ADD_ZERO_PACKET = (1U << 3)
1188 };
1189
1190 /** \ingroup libusb_asyncio
1191  * Isochronous packet descriptor. */
1192 struct libusb_iso_packet_descriptor {
1193         /** Length of data to request in this packet */
1194         unsigned int length;
1195
1196         /** Amount of data that was actually transferred */
1197         unsigned int actual_length;
1198
1199         /** Status code for this packet */
1200         enum libusb_transfer_status status;
1201 };
1202
1203 struct libusb_transfer;
1204
1205 /** \ingroup libusb_asyncio
1206  * Asynchronous transfer callback function type. When submitting asynchronous
1207  * transfers, you pass a pointer to a callback function of this type via the
1208  * \ref libusb_transfer::callback "callback" member of the libusb_transfer
1209  * structure. libusb will call this function later, when the transfer has
1210  * completed or failed. See \ref libusb_asyncio for more information.
1211  * \param transfer The libusb_transfer struct the callback function is being
1212  * notified about.
1213  */
1214 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
1215
1216 /** \ingroup libusb_asyncio
1217  * The generic USB transfer structure. The user populates this structure and
1218  * then submits it in order to request a transfer. After the transfer has
1219  * completed, the library populates the transfer with the results and passes
1220  * it back to the user.
1221  */
1222 struct libusb_transfer {
1223         /** Handle of the device that this transfer will be submitted to */
1224         libusb_device_handle *dev_handle;
1225
1226         /** A bitwise OR combination of \ref libusb_transfer_flags. */
1227         uint8_t flags;
1228
1229         /** Address of the endpoint where this transfer will be sent. */
1230         unsigned char endpoint;
1231
1232         /** Type of the transfer from \ref libusb_transfer_type */
1233         unsigned char type;
1234
1235         /** Timeout for this transfer in milliseconds. A value of 0 indicates no
1236          * timeout. */
1237         unsigned int timeout;
1238
1239         /** The status of the transfer. Read-only, and only for use within
1240          * transfer callback function.
1241          *
1242          * If this is an isochronous transfer, this field may read COMPLETED even
1243          * if there were errors in the frames. Use the
1244          * \ref libusb_iso_packet_descriptor::status "status" field in each packet
1245          * to determine if errors occurred. */
1246         enum libusb_transfer_status status;
1247
1248         /** Length of the data buffer. Must be non-negative. */
1249         int length;
1250
1251         /** Actual length of data that was transferred. Read-only, and only for
1252          * use within transfer callback function. Not valid for isochronous
1253          * endpoint transfers. */
1254         int actual_length;
1255
1256         /** Callback function. This will be invoked when the transfer completes,
1257          * fails, or is cancelled. */
1258         libusb_transfer_cb_fn callback;
1259
1260         /** User context data. Useful for associating specific data to a transfer
1261          * that can be accessed from within the callback function.
1262          *
1263          * This field may be set manually or is taken as the `user_data` parameter
1264          * of the following functions:
1265          * - libusb_fill_bulk_transfer()
1266          * - libusb_fill_bulk_stream_transfer()
1267          * - libusb_fill_control_transfer()
1268          * - libusb_fill_interrupt_transfer()
1269          * - libusb_fill_iso_transfer() */
1270         void *user_data;
1271
1272         /** Data buffer */
1273         unsigned char *buffer;
1274
1275         /** Number of isochronous packets. Only used for I/O with isochronous
1276          * endpoints. Must be non-negative. */
1277         int num_iso_packets;
1278
1279         /** Isochronous packet descriptors, for isochronous transfers only. */
1280         struct libusb_iso_packet_descriptor iso_packet_desc[ZERO_SIZED_ARRAY];
1281 };
1282
1283 /** \ingroup libusb_misc
1284  * Capabilities supported by an instance of libusb on the current running
1285  * platform. Test if the loaded library supports a given capability by calling
1286  * \ref libusb_has_capability().
1287  */
1288 enum libusb_capability {
1289         /** The libusb_has_capability() API is available. */
1290         LIBUSB_CAP_HAS_CAPABILITY = 0x0000U,
1291
1292         /** Hotplug support is available on this platform. */
1293         LIBUSB_CAP_HAS_HOTPLUG = 0x0001U,
1294
1295         /** The library can access HID devices without requiring user intervention.
1296          * Note that before being able to actually access an HID device, you may
1297          * still have to call additional libusb functions such as
1298          * \ref libusb_detach_kernel_driver(). */
1299         LIBUSB_CAP_HAS_HID_ACCESS = 0x0100U,
1300
1301         /** The library supports detaching of the default USB driver, using
1302          * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
1303         LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101U
1304 };
1305
1306 /** \ingroup libusb_lib
1307  *  Log message levels.
1308  */
1309 enum libusb_log_level {
1310         /** (0) : No messages ever emitted by the library (default) */
1311         LIBUSB_LOG_LEVEL_NONE = 0,
1312
1313         /** (1) : Error messages are emitted */
1314         LIBUSB_LOG_LEVEL_ERROR = 1,
1315
1316         /** (2) : Warning and error messages are emitted */
1317         LIBUSB_LOG_LEVEL_WARNING = 2,
1318
1319         /** (3) : Informational, warning and error messages are emitted */
1320         LIBUSB_LOG_LEVEL_INFO = 3,
1321
1322         /** (4) : All messages are emitted */
1323         LIBUSB_LOG_LEVEL_DEBUG = 4
1324 };
1325
1326 /** \ingroup libusb_lib
1327  *  Log callback mode.
1328  * \see libusb_set_log_cb()
1329  */
1330 enum libusb_log_cb_mode {
1331         /** Callback function handling all log messages. */
1332         LIBUSB_LOG_CB_GLOBAL = (1 << 0),
1333
1334         /** Callback function handling context related log messages. */
1335         LIBUSB_LOG_CB_CONTEXT = (1 << 1)
1336 };
1337
1338 /** \ingroup libusb_lib
1339  * Callback function for handling log messages.
1340  * \param ctx the context which is related to the log message, or NULL if it
1341  * is a global log message
1342  * \param level the log level, see \ref libusb_log_level for a description
1343  * \param str the log message
1344  * \see libusb_set_log_cb()
1345  */
1346 typedef void (LIBUSB_CALL *libusb_log_cb)(libusb_context *ctx,
1347         enum libusb_log_level level, const char *str);
1348
1349 int LIBUSB_CALL libusb_init(libusb_context **ctx);
1350 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
1351 LIBUSB_DEPRECATED_FOR(libusb_set_option)
1352 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1353 void LIBUSB_CALL libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode);
1354 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1355 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1356 const char * LIBUSB_CALL libusb_error_name(int errcode);
1357 int LIBUSB_CALL libusb_setlocale(const char *locale);
1358 const char * LIBUSB_CALL libusb_strerror(int errcode);
1359
1360 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
1361         libusb_device ***list);
1362 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
1363         int unref_devices);
1364 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
1365 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1366
1367 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1368         int *config);
1369 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1370         struct libusb_device_descriptor *desc);
1371 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1372         struct libusb_config_descriptor **config);
1373 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1374         uint8_t config_index, struct libusb_config_descriptor **config);
1375 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1376         uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1377 void LIBUSB_CALL libusb_free_config_descriptor(
1378         struct libusb_config_descriptor *config);
1379 int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor(
1380         libusb_context *ctx,
1381         const struct libusb_endpoint_descriptor *endpoint,
1382         struct libusb_ss_endpoint_companion_descriptor **ep_comp);
1383 void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor(
1384         struct libusb_ss_endpoint_companion_descriptor *ep_comp);
1385 int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
1386         struct libusb_bos_descriptor **bos);
1387 void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos);
1388 int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor(
1389         libusb_context *ctx,
1390         struct libusb_bos_dev_capability_descriptor *dev_cap,
1391         struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension);
1392 void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor(
1393         struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension);
1394 int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor(
1395         libusb_context *ctx,
1396         struct libusb_bos_dev_capability_descriptor *dev_cap,
1397         struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap);
1398 void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor(
1399         struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap);
1400 int LIBUSB_CALL libusb_get_container_id_descriptor(libusb_context *ctx,
1401         struct libusb_bos_dev_capability_descriptor *dev_cap,
1402         struct libusb_container_id_descriptor **container_id);
1403 void LIBUSB_CALL libusb_free_container_id_descriptor(
1404         struct libusb_container_id_descriptor *container_id);
1405 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1406 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1407 int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);
1408 LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers)
1409 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *path, uint8_t path_length);
1410 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1411 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1412 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1413 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1414         unsigned char endpoint);
1415 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1416         unsigned char endpoint);
1417
1418 int LIBUSB_CALL libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev, libusb_device_handle **dev_handle);
1419 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **dev_handle);
1420 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1421 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1422
1423 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev_handle,
1424         int configuration);
1425 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev_handle,
1426         int interface_number);
1427 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev_handle,
1428         int interface_number);
1429
1430 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1431         libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1432
1433 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev_handle,
1434         int interface_number, int alternate_setting);
1435 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev_handle,
1436         unsigned char endpoint);
1437 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev_handle);
1438
1439 int LIBUSB_CALL libusb_alloc_streams(libusb_device_handle *dev_handle,
1440         uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
1441 int LIBUSB_CALL libusb_free_streams(libusb_device_handle *dev_handle,
1442         unsigned char *endpoints, int num_endpoints);
1443
1444 unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle,
1445         size_t length);
1446 int LIBUSB_CALL libusb_dev_mem_free(libusb_device_handle *dev_handle,
1447         unsigned char *buffer, size_t length);
1448
1449 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev_handle,
1450         int interface_number);
1451 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev_handle,
1452         int interface_number);
1453 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev_handle,
1454         int interface_number);
1455 int LIBUSB_CALL libusb_set_auto_detach_kernel_driver(
1456         libusb_device_handle *dev_handle, int enable);
1457
1458 /* async I/O */
1459
1460 /** \ingroup libusb_asyncio
1461  * Get the data section of a control transfer. This convenience function is here
1462  * to remind you that the data does not start until 8 bytes into the actual
1463  * buffer, as the setup packet comes first.
1464  *
1465  * Calling this function only makes sense from a transfer callback function,
1466  * or situations where you have already allocated a suitably sized buffer at
1467  * transfer->buffer.
1468  *
1469  * \param transfer a transfer
1470  * \returns pointer to the first byte of the data section
1471  */
1472 static inline unsigned char *libusb_control_transfer_get_data(
1473         struct libusb_transfer *transfer)
1474 {
1475         return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1476 }
1477
1478 /** \ingroup libusb_asyncio
1479  * Get the control setup packet of a control transfer. This convenience
1480  * function is here to remind you that the control setup occupies the first
1481  * 8 bytes of the transfer data buffer.
1482  *
1483  * Calling this function only makes sense from a transfer callback function,
1484  * or situations where you have already allocated a suitably sized buffer at
1485  * transfer->buffer.
1486  *
1487  * \param transfer a transfer
1488  * \returns a casted pointer to the start of the transfer data buffer
1489  */
1490 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1491         struct libusb_transfer *transfer)
1492 {
1493         return (struct libusb_control_setup *)(void *)transfer->buffer;
1494 }
1495
1496 /** \ingroup libusb_asyncio
1497  * Helper function to populate the setup packet (first 8 bytes of the data
1498  * buffer) for a control transfer. The wIndex, wValue and wLength values should
1499  * be given in host-endian byte order.
1500  *
1501  * \param buffer buffer to output the setup packet into
1502  * This pointer must be aligned to at least 2 bytes boundary.
1503  * \param bmRequestType see the
1504  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1505  * \ref libusb_control_setup
1506  * \param bRequest see the
1507  * \ref libusb_control_setup::bRequest "bRequest" field of
1508  * \ref libusb_control_setup
1509  * \param wValue see the
1510  * \ref libusb_control_setup::wValue "wValue" field of
1511  * \ref libusb_control_setup
1512  * \param wIndex see the
1513  * \ref libusb_control_setup::wIndex "wIndex" field of
1514  * \ref libusb_control_setup
1515  * \param wLength see the
1516  * \ref libusb_control_setup::wLength "wLength" field of
1517  * \ref libusb_control_setup
1518  */
1519 static inline void libusb_fill_control_setup(unsigned char *buffer,
1520         uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1521         uint16_t wLength)
1522 {
1523         struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *)buffer;
1524         setup->bmRequestType = bmRequestType;
1525         setup->bRequest = bRequest;
1526         setup->wValue = libusb_cpu_to_le16(wValue);
1527         setup->wIndex = libusb_cpu_to_le16(wIndex);
1528         setup->wLength = libusb_cpu_to_le16(wLength);
1529 }
1530
1531 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1532 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1533 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1534 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1535 void LIBUSB_CALL libusb_transfer_set_stream_id(
1536         struct libusb_transfer *transfer, uint32_t stream_id);
1537 uint32_t LIBUSB_CALL libusb_transfer_get_stream_id(
1538         struct libusb_transfer *transfer);
1539
1540 /** \ingroup libusb_asyncio
1541  * Helper function to populate the required \ref libusb_transfer fields
1542  * for a control transfer.
1543  *
1544  * If you pass a transfer buffer to this function, the first 8 bytes will
1545  * be interpreted as a control setup packet, and the wLength field will be
1546  * used to automatically populate the \ref libusb_transfer::length "length"
1547  * field of the transfer. Therefore the recommended approach is:
1548  * -# Allocate a suitably sized data buffer (including space for control setup)
1549  * -# Call libusb_fill_control_setup()
1550  * -# If this is a host-to-device transfer with a data stage, put the data
1551  *    in place after the setup packet
1552  * -# Call this function
1553  * -# Call libusb_submit_transfer()
1554  *
1555  * It is also legal to pass a NULL buffer to this function, in which case this
1556  * function will not attempt to populate the length field. Remember that you
1557  * must then populate the buffer and length fields later.
1558  *
1559  * \param transfer the transfer to populate
1560  * \param dev_handle handle of the device that will handle the transfer
1561  * \param buffer data buffer. If provided, this function will interpret the
1562  * first 8 bytes as a setup packet and infer the transfer length from that.
1563  * This pointer must be aligned to at least 2 bytes boundary.
1564  * \param callback callback function to be invoked on transfer completion
1565  * \param user_data user data to pass to callback function
1566  * \param timeout timeout for the transfer in milliseconds
1567  */
1568 static inline void libusb_fill_control_transfer(
1569         struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1570         unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1571         unsigned int timeout)
1572 {
1573         struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *)buffer;
1574         transfer->dev_handle = dev_handle;
1575         transfer->endpoint = 0;
1576         transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1577         transfer->timeout = timeout;
1578         transfer->buffer = buffer;
1579         if (setup)
1580                 transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE
1581                         + libusb_le16_to_cpu(setup->wLength));
1582         transfer->user_data = user_data;
1583         transfer->callback = callback;
1584 }
1585
1586 /** \ingroup libusb_asyncio
1587  * Helper function to populate the required \ref libusb_transfer fields
1588  * for a bulk transfer.
1589  *
1590  * \param transfer the transfer to populate
1591  * \param dev_handle handle of the device that will handle the transfer
1592  * \param endpoint address of the endpoint where this transfer will be sent
1593  * \param buffer data buffer
1594  * \param length length of data buffer
1595  * \param callback callback function to be invoked on transfer completion
1596  * \param user_data user data to pass to callback function
1597  * \param timeout timeout for the transfer in milliseconds
1598  */
1599 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1600         libusb_device_handle *dev_handle, unsigned char endpoint,
1601         unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1602         void *user_data, unsigned int timeout)
1603 {
1604         transfer->dev_handle = dev_handle;
1605         transfer->endpoint = endpoint;
1606         transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1607         transfer->timeout = timeout;
1608         transfer->buffer = buffer;
1609         transfer->length = length;
1610         transfer->user_data = user_data;
1611         transfer->callback = callback;
1612 }
1613
1614 /** \ingroup libusb_asyncio
1615  * Helper function to populate the required \ref libusb_transfer fields
1616  * for a bulk transfer using bulk streams.
1617  *
1618  * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
1619  *
1620  * \param transfer the transfer to populate
1621  * \param dev_handle handle of the device that will handle the transfer
1622  * \param endpoint address of the endpoint where this transfer will be sent
1623  * \param stream_id bulk stream id for this transfer
1624  * \param buffer data buffer
1625  * \param length length of data buffer
1626  * \param callback callback function to be invoked on transfer completion
1627  * \param user_data user data to pass to callback function
1628  * \param timeout timeout for the transfer in milliseconds
1629  */
1630 static inline void libusb_fill_bulk_stream_transfer(
1631         struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1632         unsigned char endpoint, uint32_t stream_id,
1633         unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1634         void *user_data, unsigned int timeout)
1635 {
1636         libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer,
1637                                   length, callback, user_data, timeout);
1638         transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
1639         libusb_transfer_set_stream_id(transfer, stream_id);
1640 }
1641
1642 /** \ingroup libusb_asyncio
1643  * Helper function to populate the required \ref libusb_transfer fields
1644  * for an interrupt transfer.
1645  *
1646  * \param transfer the transfer to populate
1647  * \param dev_handle handle of the device that will handle the transfer
1648  * \param endpoint address of the endpoint where this transfer will be sent
1649  * \param buffer data buffer
1650  * \param length length of data buffer
1651  * \param callback callback function to be invoked on transfer completion
1652  * \param user_data user data to pass to callback function
1653  * \param timeout timeout for the transfer in milliseconds
1654  */
1655 static inline void libusb_fill_interrupt_transfer(
1656         struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1657         unsigned char endpoint, unsigned char *buffer, int length,
1658         libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1659 {
1660         transfer->dev_handle = dev_handle;
1661         transfer->endpoint = endpoint;
1662         transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1663         transfer->timeout = timeout;
1664         transfer->buffer = buffer;
1665         transfer->length = length;
1666         transfer->user_data = user_data;
1667         transfer->callback = callback;
1668 }
1669
1670 /** \ingroup libusb_asyncio
1671  * Helper function to populate the required \ref libusb_transfer fields
1672  * for an isochronous transfer.
1673  *
1674  * \param transfer the transfer to populate
1675  * \param dev_handle handle of the device that will handle the transfer
1676  * \param endpoint address of the endpoint where this transfer will be sent
1677  * \param buffer data buffer
1678  * \param length length of data buffer
1679  * \param num_iso_packets the number of isochronous packets
1680  * \param callback callback function to be invoked on transfer completion
1681  * \param user_data user data to pass to callback function
1682  * \param timeout timeout for the transfer in milliseconds
1683  */
1684 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1685         libusb_device_handle *dev_handle, unsigned char endpoint,
1686         unsigned char *buffer, int length, int num_iso_packets,
1687         libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1688 {
1689         transfer->dev_handle = dev_handle;
1690         transfer->endpoint = endpoint;
1691         transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1692         transfer->timeout = timeout;
1693         transfer->buffer = buffer;
1694         transfer->length = length;
1695         transfer->num_iso_packets = num_iso_packets;
1696         transfer->user_data = user_data;
1697         transfer->callback = callback;
1698 }
1699
1700 /** \ingroup libusb_asyncio
1701  * Convenience function to set the length of all packets in an isochronous
1702  * transfer, based on the num_iso_packets field in the transfer structure.
1703  *
1704  * \param transfer a transfer
1705  * \param length the length to set in each isochronous packet descriptor
1706  * \see libusb_get_max_packet_size()
1707  */
1708 static inline void libusb_set_iso_packet_lengths(
1709         struct libusb_transfer *transfer, unsigned int length)
1710 {
1711         int i;
1712
1713         for (i = 0; i < transfer->num_iso_packets; i++)
1714                 transfer->iso_packet_desc[i].length = length;
1715 }
1716
1717 /** \ingroup libusb_asyncio
1718  * Convenience function to locate the position of an isochronous packet
1719  * within the buffer of an isochronous transfer.
1720  *
1721  * This is a thorough function which loops through all preceding packets,
1722  * accumulating their lengths to find the position of the specified packet.
1723  * Typically you will assign equal lengths to each packet in the transfer,
1724  * and hence the above method is sub-optimal. You may wish to use
1725  * libusb_get_iso_packet_buffer_simple() instead.
1726  *
1727  * \param transfer a transfer
1728  * \param packet the packet to return the address of
1729  * \returns the base address of the packet buffer inside the transfer buffer,
1730  * or NULL if the packet does not exist.
1731  * \see libusb_get_iso_packet_buffer_simple()
1732  */
1733 static inline unsigned char *libusb_get_iso_packet_buffer(
1734         struct libusb_transfer *transfer, unsigned int packet)
1735 {
1736         int i;
1737         size_t offset = 0;
1738         int _packet;
1739
1740         /* oops..slight bug in the API. packet is an unsigned int, but we use
1741          * signed integers almost everywhere else. range-check and convert to
1742          * signed to avoid compiler warnings. FIXME for libusb-2. */
1743         if (packet > INT_MAX)
1744                 return NULL;
1745         _packet = (int) packet;
1746
1747         if (_packet >= transfer->num_iso_packets)
1748                 return NULL;
1749
1750         for (i = 0; i < _packet; i++)
1751                 offset += transfer->iso_packet_desc[i].length;
1752
1753         return transfer->buffer + offset;
1754 }
1755
1756 /** \ingroup libusb_asyncio
1757  * Convenience function to locate the position of an isochronous packet
1758  * within the buffer of an isochronous transfer, for transfers where each
1759  * packet is of identical size.
1760  *
1761  * This function relies on the assumption that every packet within the transfer
1762  * is of identical size to the first packet. Calculating the location of
1763  * the packet buffer is then just a simple calculation:
1764  * <tt>buffer + (packet_size * packet)</tt>
1765  *
1766  * Do not use this function on transfers other than those that have identical
1767  * packet lengths for each packet.
1768  *
1769  * \param transfer a transfer
1770  * \param packet the packet to return the address of
1771  * \returns the base address of the packet buffer inside the transfer buffer,
1772  * or NULL if the packet does not exist.
1773  * \see libusb_get_iso_packet_buffer()
1774  */
1775 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1776         struct libusb_transfer *transfer, unsigned int packet)
1777 {
1778         int _packet;
1779
1780         /* oops..slight bug in the API. packet is an unsigned int, but we use
1781          * signed integers almost everywhere else. range-check and convert to
1782          * signed to avoid compiler warnings. FIXME for libusb-2. */
1783         if (packet > INT_MAX)
1784                 return NULL;
1785         _packet = (int) packet;
1786
1787         if (_packet >= transfer->num_iso_packets)
1788                 return NULL;
1789
1790         return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet);
1791 }
1792
1793 /* sync I/O */
1794
1795 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1796         uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1797         unsigned char *data, uint16_t wLength, unsigned int timeout);
1798
1799 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1800         unsigned char endpoint, unsigned char *data, int length,
1801         int *actual_length, unsigned int timeout);
1802
1803 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1804         unsigned char endpoint, unsigned char *data, int length,
1805         int *actual_length, unsigned int timeout);
1806
1807 /** \ingroup libusb_desc
1808  * Retrieve a descriptor from the default control pipe.
1809  * This is a convenience function which formulates the appropriate control
1810  * message to retrieve the descriptor.
1811  *
1812  * \param dev_handle a device handle
1813  * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1814  * \param desc_index the index of the descriptor to retrieve
1815  * \param data output buffer for descriptor
1816  * \param length size of data buffer
1817  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1818  */
1819 static inline int libusb_get_descriptor(libusb_device_handle *dev_handle,
1820         uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1821 {
1822         return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
1823                 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index),
1824                 0, data, (uint16_t) length, 1000);
1825 }
1826
1827 /** \ingroup libusb_desc
1828  * Retrieve a descriptor from a device.
1829  * This is a convenience function which formulates the appropriate control
1830  * message to retrieve the descriptor. The string returned is Unicode, as
1831  * detailed in the USB specifications.
1832  *
1833  * \param dev_handle a device handle
1834  * \param desc_index the index of the descriptor to retrieve
1835  * \param langid the language ID for the string descriptor
1836  * \param data output buffer for descriptor
1837  * \param length size of data buffer
1838  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1839  * \see libusb_get_string_descriptor_ascii()
1840  */
1841 static inline int libusb_get_string_descriptor(libusb_device_handle *dev_handle,
1842         uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1843 {
1844         return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
1845                 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1846                 langid, data, (uint16_t) length, 1000);
1847 }
1848
1849 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
1850         uint8_t desc_index, unsigned char *data, int length);
1851
1852 /* polling and timeouts */
1853
1854 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1855 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1856 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1857 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1858 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1859 void LIBUSB_CALL libusb_interrupt_event_handler(libusb_context *ctx);
1860 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1861 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1862 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1863
1864 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1865         struct timeval *tv);
1866 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1867         struct timeval *tv, int *completed);
1868 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1869 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1870 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1871         struct timeval *tv);
1872 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1873 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1874         struct timeval *tv);
1875
1876 /** \ingroup libusb_poll
1877  * File descriptor for polling
1878  */
1879 struct libusb_pollfd {
1880         /** Numeric file descriptor */
1881         int fd;
1882
1883         /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1884          * should monitor this file descriptor for becoming ready to read from,
1885          * and POLLOUT indicates that you should monitor this file descriptor for
1886          * nonblocking write readiness. */
1887         short events;
1888 };
1889
1890 /** \ingroup libusb_poll
1891  * Callback function, invoked when a new file descriptor should be added
1892  * to the set of file descriptors monitored for events.
1893  * \param fd the new file descriptor
1894  * \param events events to monitor for, see \ref libusb_pollfd for a
1895  * description
1896  * \param user_data User data pointer specified in
1897  * libusb_set_pollfd_notifiers() call
1898  * \see libusb_set_pollfd_notifiers()
1899  */
1900 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1901         void *user_data);
1902
1903 /** \ingroup libusb_poll
1904  * Callback function, invoked when a file descriptor should be removed from
1905  * the set of file descriptors being monitored for events. After returning
1906  * from this callback, do not use that file descriptor again.
1907  * \param fd the file descriptor to stop monitoring
1908  * \param user_data User data pointer specified in
1909  * libusb_set_pollfd_notifiers() call
1910  * \see libusb_set_pollfd_notifiers()
1911  */
1912 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1913
1914 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1915         libusb_context *ctx);
1916 void LIBUSB_CALL libusb_free_pollfds(const struct libusb_pollfd **pollfds);
1917 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1918         libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1919         void *user_data);
1920
1921 /** \ingroup libusb_hotplug
1922  * Callback handle.
1923  *
1924  * Callbacks handles are generated by libusb_hotplug_register_callback()
1925  * and can be used to deregister callbacks. Callback handles are unique
1926  * per libusb_context and it is safe to call libusb_hotplug_deregister_callback()
1927  * on an already deregistered callback.
1928  *
1929  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1930  *
1931  * For more information, see \ref libusb_hotplug.
1932  */
1933 typedef int libusb_hotplug_callback_handle;
1934
1935 /** \ingroup libusb_hotplug
1936  *
1937  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1938  *
1939  * Hotplug events */
1940 typedef enum {
1941         /** A device has been plugged in and is ready to use */
1942         LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = (1 << 0),
1943
1944         /** A device has left and is no longer available.
1945          * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
1946          * It is safe to call libusb_get_device_descriptor on a device that has left */
1947         LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = (1 << 1)
1948 } libusb_hotplug_event;
1949
1950 /** \ingroup libusb_hotplug
1951  *
1952  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1953  *
1954  * Hotplug flags */
1955 typedef enum {
1956         /** Arm the callback and fire it for all matching currently attached devices. */
1957         LIBUSB_HOTPLUG_ENUMERATE = (1 << 0)
1958 } libusb_hotplug_flag;
1959
1960 /** \ingroup libusb_hotplug
1961  * Convenience macro when not using any flags */
1962 #define LIBUSB_HOTPLUG_NO_FLAGS 0
1963
1964 /** \ingroup libusb_hotplug
1965  * Wildcard matching for hotplug events */
1966 #define LIBUSB_HOTPLUG_MATCH_ANY -1
1967
1968 /** \ingroup libusb_hotplug
1969  * Hotplug callback function type. When requesting hotplug event notifications,
1970  * you pass a pointer to a callback function of this type.
1971  *
1972  * This callback may be called by an internal event thread and as such it is
1973  * recommended the callback do minimal processing before returning.
1974  *
1975  * libusb will call this function later, when a matching event had happened on
1976  * a matching device. See \ref libusb_hotplug for more information.
1977  *
1978  * It is safe to call either libusb_hotplug_register_callback() or
1979  * libusb_hotplug_deregister_callback() from within a callback function.
1980  *
1981  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1982  *
1983  * \param ctx            context of this notification
1984  * \param device         libusb_device this event occurred on
1985  * \param event          event that occurred
1986  * \param user_data      user data provided when this callback was registered
1987  * \returns bool whether this callback is finished processing events.
1988  *                       returning 1 will cause this callback to be deregistered
1989  */
1990 typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx,
1991         libusb_device *device, libusb_hotplug_event event, void *user_data);
1992
1993 /** \ingroup libusb_hotplug
1994  * Register a hotplug callback function
1995  *
1996  * Register a callback with the libusb_context. The callback will fire
1997  * when a matching event occurs on a matching device. The callback is
1998  * armed until either it is deregistered with libusb_hotplug_deregister_callback()
1999  * or the supplied callback returns 1 to indicate it is finished processing events.
2000  *
2001  * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be
2002  * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices
2003  * already plugged into the machine. Note that libusb modifies its internal
2004  * device list from a separate thread, while calling hotplug callbacks from
2005  * libusb_handle_events(), so it is possible for a device to already be present
2006  * on, or removed from, its internal device list, while the hotplug callbacks
2007  * still need to be dispatched. This means that when using \ref
2008  * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival
2009  * of the same device, once from libusb_hotplug_register_callback() and once
2010  * from libusb_handle_events(); and/or your callback may be called for the
2011  * removal of a device for which an arrived call was never made.
2012  *
2013  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
2014  *
2015  * \param[in] ctx context to register this callback with
2016  * \param[in] events bitwise or of hotplug events that will trigger this callback.
2017  *            See \ref libusb_hotplug_event
2018  * \param[in] flags bitwise or of hotplug flags that affect registration.
2019  *            See \ref libusb_hotplug_flag
2020  * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2021  * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2022  * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2023  * \param[in] cb_fn the function to be invoked on a matching event/device
2024  * \param[in] user_data user data to pass to the callback function
2025  * \param[out] callback_handle pointer to store the handle of the allocated callback (can be NULL)
2026  * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure
2027  */
2028 int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx,
2029         int events, int flags,
2030         int vendor_id, int product_id, int dev_class,
2031         libusb_hotplug_callback_fn cb_fn, void *user_data,
2032         libusb_hotplug_callback_handle *callback_handle);
2033
2034 /** \ingroup libusb_hotplug
2035  * Deregisters a hotplug callback.
2036  *
2037  * Deregister a callback from a libusb_context. This function is safe to call from within
2038  * a hotplug callback.
2039  *
2040  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
2041  *
2042  * \param[in] ctx context this callback is registered with
2043  * \param[in] callback_handle the handle of the callback to deregister
2044  */
2045 void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx,
2046         libusb_hotplug_callback_handle callback_handle);
2047
2048 /** \ingroup libusb_hotplug
2049  * Gets the user_data associated with a hotplug callback.
2050  *
2051  * Since version v1.0.24 \ref LIBUSB_API_VERSION >= 0x01000108
2052  *
2053  * \param[in] ctx context this callback is registered with
2054  * \param[in] callback_handle the handle of the callback to get the user_data of
2055  */
2056 void * LIBUSB_CALL libusb_hotplug_get_user_data(libusb_context *ctx,
2057         libusb_hotplug_callback_handle callback_handle);
2058
2059 /** \ingroup libusb_lib
2060  * Available option values for libusb_set_option().
2061  */
2062 enum libusb_option {
2063         /** Set the log message verbosity.
2064          *
2065          * The default level is LIBUSB_LOG_LEVEL_NONE, which means no messages are ever
2066          * printed. If you choose to increase the message verbosity level, ensure
2067          * that your application does not close the stderr file descriptor.
2068          *
2069          * You are advised to use level LIBUSB_LOG_LEVEL_WARNING. libusb is conservative
2070          * with its message logging and most of the time, will only log messages that
2071          * explain error conditions and other oddities. This will help you debug
2072          * your software.
2073          *
2074          * If the LIBUSB_DEBUG environment variable was set when libusb was
2075          * initialized, this function does nothing: the message verbosity is fixed
2076          * to the value in the environment variable.
2077          *
2078          * If libusb was compiled without any message logging, this function does
2079          * nothing: you'll never get any messages.
2080          *
2081          * If libusb was compiled with verbose debug message logging, this function
2082          * does nothing: you'll always get messages from all levels.
2083          */
2084         LIBUSB_OPTION_LOG_LEVEL = 0,
2085
2086         /** Use the UsbDk backend for a specific context, if available.
2087          *
2088          * This option should be set immediately after calling libusb_init(), otherwise
2089          * unspecified behavior may occur.
2090          *
2091          * Only valid on Windows.
2092          */
2093         LIBUSB_OPTION_USE_USBDK = 1,
2094
2095         /** Set libusb has weak authority. With this option, libusb will skip
2096          * scan devices in libusb_init.
2097          *
2098          * This option should be set before calling libusb_init(), otherwise
2099          * libusb_init will failed. Normally libusb_wrap_sys_device need set
2100          * this option.
2101          *
2102          * Only valid on Linux-based operating system, such as Android.
2103          */
2104         LIBUSB_OPTION_WEAK_AUTHORITY = 2
2105 };
2106
2107 int LIBUSB_CALL libusb_set_option(libusb_context *ctx, enum libusb_option option, ...);
2108
2109 #if defined(__cplusplus)
2110 }
2111 #endif
2112
2113 #endif