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