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