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