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