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