Core: Make libusb_error_name also handle transfer status codes
[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  * For more information, please visit: http://libusbx.org
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #ifndef LIBUSB_H
24 #define LIBUSB_H
25
26 #ifdef _MSC_VER
27 /* on MS environments, the inline keyword is available in C++ only */
28 #if !defined(__cplusplus)
29 #define inline __inline
30 #endif
31 /* ssize_t is also not available (copy/paste from MinGW) */
32 #ifndef _SSIZE_T_DEFINED
33 #define _SSIZE_T_DEFINED
34 #undef ssize_t
35 #ifdef _WIN64
36   typedef __int64 ssize_t;
37 #else
38   typedef int ssize_t;
39 #endif /* _WIN64 */
40 #endif /* _SSIZE_T_DEFINED */
41 #endif /* _MSC_VER */
42
43 /* stdint.h is also not usually available on MS */
44 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
45 typedef unsigned __int8   uint8_t;
46 typedef unsigned __int16  uint16_t;
47 typedef unsigned __int32  uint32_t;
48 #else
49 #include <stdint.h>
50 #endif
51
52 #include <sys/types.h>
53 #include <time.h>
54 #include <limits.h>
55
56 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
57 #include <sys/time.h>
58 #endif
59
60 /* 'interface' might be defined as a macro on Windows, so we need to
61  * undefine it so as not to break the current libusbx API, because
62  * libusb_config_descriptor has an 'interface' member
63  * As this can be problematic if you include windows.h after libusb.h
64  * in your sources, we force windows.h to be included first. */
65 #if defined(_WIN32) || defined(__CYGWIN__)
66 #include <windows.h>
67 #if defined(interface)
68 #undef interface
69 #endif
70 #endif
71
72 /** \def LIBUSB_CALL
73  * \ingroup misc
74  * libusbx's Windows calling convention.
75  *
76  * Under Windows, the selection of available compilers and configurations
77  * means that, unlike other platforms, there is not <em>one true calling
78  * convention</em> (calling convention: the manner in which parameters are
79  * passed to funcions in the generated assembly code).
80  *
81  * Matching the Windows API itself, libusbx uses the WINAPI convention (which
82  * translates to the <tt>stdcall</tt> convention) and guarantees that the
83  * library is compiled in this way. The public header file also includes
84  * appropriate annotations so that your own software will use the right
85  * convention, even if another convention is being used by default within
86  * your codebase.
87  *
88  * The one consideration that you must apply in your software is to mark
89  * all functions which you use as libusbx callbacks with this LIBUSB_CALL
90  * annotation, so that they too get compiled for the correct calling
91  * convention.
92  *
93  * On non-Windows operating systems, this macro is defined as nothing. This
94  * means that you can apply it to your code without worrying about
95  * cross-platform compatibility.
96  */
97 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx
98  * functions. You'd think that declaration would be enough, but cygwin will
99  * complain about conflicting types unless both are marked this way.
100  * The placement of this macro is important too; it must appear after the
101  * return type, before the function name. See internal documentation for
102  * API_EXPORTED.
103  */
104 #if defined(_WIN32) || defined(__CYGWIN__)
105 #define LIBUSB_CALL WINAPI
106 #else
107 #define LIBUSB_CALL
108 #endif
109
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113
114 /** \def libusb_cpu_to_le16
115  * \ingroup misc
116  * Convert a 16-bit value from host-endian to little-endian format. On
117  * little endian systems, this function does nothing. On big endian systems,
118  * the bytes are swapped.
119  * \param x the host-endian value to convert
120  * \returns the value in little-endian byte order
121  */
122 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
123 {
124         union {
125                 uint8_t  b8[2];
126                 uint16_t b16;
127         } _tmp;
128         _tmp.b8[1] = x >> 8;
129         _tmp.b8[0] = x & 0xff;
130         return _tmp.b16;
131 }
132
133 /** \def libusb_le16_to_cpu
134  * \ingroup misc
135  * Convert a 16-bit value from little-endian to host-endian format. On
136  * little endian systems, this function does nothing. On big endian systems,
137  * the bytes are swapped.
138  * \param x the little-endian value to convert
139  * \returns the value in host-endian byte order
140  */
141 #define libusb_le16_to_cpu libusb_cpu_to_le16
142
143 /* standard USB stuff */
144
145 /** \ingroup desc
146  * Device and/or Interface Class codes */
147 enum libusb_class_code {
148         /** In the context of a \ref libusb_device_descriptor "device descriptor",
149          * this bDeviceClass value indicates that each interface specifies its
150          * own class information and all interfaces operate independently.
151          */
152         LIBUSB_CLASS_PER_INTERFACE = 0,
153
154         /** Audio class */
155         LIBUSB_CLASS_AUDIO = 1,
156
157         /** Communications class */
158         LIBUSB_CLASS_COMM = 2,
159
160         /** Human Interface Device class */
161         LIBUSB_CLASS_HID = 3,
162
163         /** Physical */
164         LIBUSB_CLASS_PHYSICAL = 5,
165
166         /** Printer class */
167         LIBUSB_CLASS_PRINTER = 7,
168
169         /** Image class */
170         LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
171         LIBUSB_CLASS_IMAGE = 6,
172
173         /** Mass storage class */
174         LIBUSB_CLASS_MASS_STORAGE = 8,
175
176         /** Hub class */
177         LIBUSB_CLASS_HUB = 9,
178
179         /** Data class */
180         LIBUSB_CLASS_DATA = 10,
181
182         /** Smart Card */
183         LIBUSB_CLASS_SMART_CARD = 0x0b,
184
185         /** Content Security */
186         LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
187
188         /** Video */
189         LIBUSB_CLASS_VIDEO = 0x0e,
190
191         /** Personal Healthcare */
192         LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
193
194         /** Diagnostic Device */
195         LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
196
197         /** Wireless class */
198         LIBUSB_CLASS_WIRELESS = 0xe0,
199
200         /** Application class */
201         LIBUSB_CLASS_APPLICATION = 0xfe,
202
203         /** Class is vendor-specific */
204         LIBUSB_CLASS_VENDOR_SPEC = 0xff
205 };
206
207 /** \ingroup desc
208  * Descriptor types as defined by the USB specification. */
209 enum libusb_descriptor_type {
210         /** Device descriptor. See libusb_device_descriptor. */
211         LIBUSB_DT_DEVICE = 0x01,
212
213         /** Configuration descriptor. See libusb_config_descriptor. */
214         LIBUSB_DT_CONFIG = 0x02,
215
216         /** String descriptor */
217         LIBUSB_DT_STRING = 0x03,
218
219         /** Interface descriptor. See libusb_interface_descriptor. */
220         LIBUSB_DT_INTERFACE = 0x04,
221
222         /** Endpoint descriptor. See libusb_endpoint_descriptor. */
223         LIBUSB_DT_ENDPOINT = 0x05,
224
225         /** HID descriptor */
226         LIBUSB_DT_HID = 0x21,
227
228         /** HID report descriptor */
229         LIBUSB_DT_REPORT = 0x22,
230
231         /** Physical descriptor */
232         LIBUSB_DT_PHYSICAL = 0x23,
233
234         /** Hub descriptor */
235         LIBUSB_DT_HUB = 0x29,
236
237         /** SuperSpeed Hub descriptor */
238         LIBUSB_DT_SUPERSPEED_HUB = 0x2A,
239 };
240
241 /* Descriptor sizes per descriptor type */
242 #define LIBUSB_DT_DEVICE_SIZE                   18
243 #define LIBUSB_DT_CONFIG_SIZE                   9
244 #define LIBUSB_DT_INTERFACE_SIZE                9
245 #define LIBUSB_DT_ENDPOINT_SIZE         7
246 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE   9       /* Audio extension */
247 #define LIBUSB_DT_HUB_NONVAR_SIZE               7
248
249 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
250 #define LIBUSB_ENDPOINT_DIR_MASK                0x80
251
252 /** \ingroup desc
253  * Endpoint direction. Values for bit 7 of the
254  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
255  */
256 enum libusb_endpoint_direction {
257         /** In: device-to-host */
258         LIBUSB_ENDPOINT_IN = 0x80,
259
260         /** Out: host-to-device */
261         LIBUSB_ENDPOINT_OUT = 0x00
262 };
263
264 #define LIBUSB_TRANSFER_TYPE_MASK                       0x03    /* in bmAttributes */
265
266 /** \ingroup desc
267  * Endpoint transfer type. Values for bits 0:1 of the
268  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
269  */
270 enum libusb_transfer_type {
271         /** Control endpoint */
272         LIBUSB_TRANSFER_TYPE_CONTROL = 0,
273
274         /** Isochronous endpoint */
275         LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
276
277         /** Bulk endpoint */
278         LIBUSB_TRANSFER_TYPE_BULK = 2,
279
280         /** Interrupt endpoint */
281         LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
282 };
283
284 /** \ingroup misc
285  * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
286 enum libusb_standard_request {
287         /** Request status of the specific recipient */
288         LIBUSB_REQUEST_GET_STATUS = 0x00,
289
290         /** Clear or disable a specific feature */
291         LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
292
293         /* 0x02 is reserved */
294
295         /** Set or enable a specific feature */
296         LIBUSB_REQUEST_SET_FEATURE = 0x03,
297
298         /* 0x04 is reserved */
299
300         /** Set device address for all future accesses */
301         LIBUSB_REQUEST_SET_ADDRESS = 0x05,
302
303         /** Get the specified descriptor */
304         LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
305
306         /** Used to update existing descriptors or add new descriptors */
307         LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
308
309         /** Get the current device configuration value */
310         LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
311
312         /** Set device configuration */
313         LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
314
315         /** Return the selected alternate setting for the specified interface */
316         LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
317
318         /** Select an alternate interface for the specified interface */
319         LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
320
321         /** Set then report an endpoint's synchronization frame */
322         LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
323
324         /** Sets both the U1 and U2 Exit Latency */
325         LIBUSB_REQUEST_SET_SEL = 0x30,
326
327         /** Delay from the time a host transmits a packet to the time it is
328           * received by the device. */
329         LIBUSB_SET_ISOCH_DELAY = 0x31,
330 };
331
332 /** \ingroup misc
333  * Request type bits of the
334  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
335  * transfers. */
336 enum libusb_request_type {
337         /** Standard */
338         LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
339
340         /** Class */
341         LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
342
343         /** Vendor */
344         LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
345
346         /** Reserved */
347         LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
348 };
349
350 /** \ingroup misc
351  * Recipient bits of the
352  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
353  * transfers. Values 4 through 31 are reserved. */
354 enum libusb_request_recipient {
355         /** Device */
356         LIBUSB_RECIPIENT_DEVICE = 0x00,
357
358         /** Interface */
359         LIBUSB_RECIPIENT_INTERFACE = 0x01,
360
361         /** Endpoint */
362         LIBUSB_RECIPIENT_ENDPOINT = 0x02,
363
364         /** Other */
365         LIBUSB_RECIPIENT_OTHER = 0x03,
366 };
367
368 #define LIBUSB_ISO_SYNC_TYPE_MASK               0x0C
369
370 /** \ingroup desc
371  * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
372  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
373  * libusb_endpoint_descriptor.
374  */
375 enum libusb_iso_sync_type {
376         /** No synchronization */
377         LIBUSB_ISO_SYNC_TYPE_NONE = 0,
378
379         /** Asynchronous */
380         LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
381
382         /** Adaptive */
383         LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
384
385         /** Synchronous */
386         LIBUSB_ISO_SYNC_TYPE_SYNC = 3
387 };
388
389 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
390
391 /** \ingroup desc
392  * Usage type for isochronous endpoints. Values for bits 4:5 of the
393  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
394  * libusb_endpoint_descriptor.
395  */
396 enum libusb_iso_usage_type {
397         /** Data endpoint */
398         LIBUSB_ISO_USAGE_TYPE_DATA = 0,
399
400         /** Feedback endpoint */
401         LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
402
403         /** Implicit feedback Data endpoint */
404         LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
405 };
406
407 /** \ingroup desc
408  * A structure representing the standard USB device descriptor. This
409  * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
410  * All multiple-byte fields are represented in host-endian format.
411  */
412 struct libusb_device_descriptor {
413         /** Size of this descriptor (in bytes) */
414         uint8_t  bLength;
415
416         /** Descriptor type. Will have value
417          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
418          * context. */
419         uint8_t  bDescriptorType;
420
421         /** USB specification release number in binary-coded decimal. A value of
422          * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
423         uint16_t bcdUSB;
424
425         /** USB-IF class code for the device. See \ref libusb_class_code. */
426         uint8_t  bDeviceClass;
427
428         /** USB-IF subclass code for the device, qualified by the bDeviceClass
429          * value */
430         uint8_t  bDeviceSubClass;
431
432         /** USB-IF protocol code for the device, qualified by the bDeviceClass and
433          * bDeviceSubClass values */
434         uint8_t  bDeviceProtocol;
435
436         /** Maximum packet size for endpoint 0 */
437         uint8_t  bMaxPacketSize0;
438
439         /** USB-IF vendor ID */
440         uint16_t idVendor;
441
442         /** USB-IF product ID */
443         uint16_t idProduct;
444
445         /** Device release number in binary-coded decimal */
446         uint16_t bcdDevice;
447
448         /** Index of string descriptor describing manufacturer */
449         uint8_t  iManufacturer;
450
451         /** Index of string descriptor describing product */
452         uint8_t  iProduct;
453
454         /** Index of string descriptor containing device serial number */
455         uint8_t  iSerialNumber;
456
457         /** Number of possible configurations */
458         uint8_t  bNumConfigurations;
459 };
460
461 /** \ingroup desc
462  * A structure representing the standard USB endpoint descriptor. This
463  * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
464  * All multiple-byte fields are represented in host-endian format.
465  */
466 struct libusb_endpoint_descriptor {
467         /** Size of this descriptor (in bytes) */
468         uint8_t  bLength;
469
470         /** Descriptor type. Will have value
471          * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
472          * this context. */
473         uint8_t  bDescriptorType;
474
475         /** The address of the endpoint described by this descriptor. Bits 0:3 are
476          * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
477          * see \ref libusb_endpoint_direction.
478          */
479         uint8_t  bEndpointAddress;
480
481         /** Attributes which apply to the endpoint when it is configured using
482          * the bConfigurationValue. Bits 0:1 determine the transfer type and
483          * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
484          * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
485          * Bits 4:5 are also only used for isochronous endpoints and correspond to
486          * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
487          */
488         uint8_t  bmAttributes;
489
490         /** Maximum packet size this endpoint is capable of sending/receiving. */
491         uint16_t wMaxPacketSize;
492
493         /** Interval for polling endpoint for data transfers. */
494         uint8_t  bInterval;
495
496         /** For audio devices only: the rate at which synchronization feedback
497          * is provided. */
498         uint8_t  bRefresh;
499
500         /** For audio devices only: the address if the synch endpoint */
501         uint8_t  bSynchAddress;
502
503         /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
504          * it will store them here, should you wish to parse them. */
505         const unsigned char *extra;
506
507         /** Length of the extra descriptors, in bytes. */
508         int extra_length;
509 };
510
511 /** \ingroup desc
512  * A structure representing the standard USB interface descriptor. This
513  * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
514  * All multiple-byte fields are represented in host-endian format.
515  */
516 struct libusb_interface_descriptor {
517         /** Size of this descriptor (in bytes) */
518         uint8_t  bLength;
519
520         /** Descriptor type. Will have value
521          * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
522          * in this context. */
523         uint8_t  bDescriptorType;
524
525         /** Number of this interface */
526         uint8_t  bInterfaceNumber;
527
528         /** Value used to select this alternate setting for this interface */
529         uint8_t  bAlternateSetting;
530
531         /** Number of endpoints used by this interface (excluding the control
532          * endpoint). */
533         uint8_t  bNumEndpoints;
534
535         /** USB-IF class code for this interface. See \ref libusb_class_code. */
536         uint8_t  bInterfaceClass;
537
538         /** USB-IF subclass code for this interface, qualified by the
539          * bInterfaceClass value */
540         uint8_t  bInterfaceSubClass;
541
542         /** USB-IF protocol code for this interface, qualified by the
543          * bInterfaceClass and bInterfaceSubClass values */
544         uint8_t  bInterfaceProtocol;
545
546         /** Index of string descriptor describing this interface */
547         uint8_t  iInterface;
548
549         /** Array of endpoint descriptors. This length of this array is determined
550          * by the bNumEndpoints field. */
551         const struct libusb_endpoint_descriptor *endpoint;
552
553         /** Extra descriptors. If libusbx encounters unknown interface descriptors,
554          * it will store them here, should you wish to parse them. */
555         const unsigned char *extra;
556
557         /** Length of the extra descriptors, in bytes. */
558         int extra_length;
559 };
560
561 /** \ingroup desc
562  * A collection of alternate settings for a particular USB interface.
563  */
564 struct libusb_interface {
565         /** Array of interface descriptors. The length of this array is determined
566          * by the num_altsetting field. */
567         const struct libusb_interface_descriptor *altsetting;
568
569         /** The number of alternate settings that belong to this interface */
570         int num_altsetting;
571 };
572
573 /** \ingroup desc
574  * A structure representing the standard USB configuration descriptor. This
575  * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
576  * All multiple-byte fields are represented in host-endian format.
577  */
578 struct libusb_config_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_CONFIG LIBUSB_DT_CONFIG
584          * in this context. */
585         uint8_t  bDescriptorType;
586
587         /** Total length of data returned for this configuration */
588         uint16_t wTotalLength;
589
590         /** Number of interfaces supported by this configuration */
591         uint8_t  bNumInterfaces;
592
593         /** Identifier value for this configuration */
594         uint8_t  bConfigurationValue;
595
596         /** Index of string descriptor describing this configuration */
597         uint8_t  iConfiguration;
598
599         /** Configuration characteristics */
600         uint8_t  bmAttributes;
601
602         /** Maximum power consumption of the USB device from this bus in this
603          * configuration when the device is fully opreation. Expressed in units
604          * of 2 mA. */
605         uint8_t  bMaxPower;
606
607         /** Array of interfaces supported by this configuration. The length of
608          * this array is determined by the bNumInterfaces field. */
609         const struct libusb_interface *interface;
610
611         /** Extra descriptors. If libusbx encounters unknown configuration
612          * descriptors, it will store them here, should you wish to parse them. */
613         const unsigned char *extra;
614
615         /** Length of the extra descriptors, in bytes. */
616         int extra_length;
617 };
618
619 /** \ingroup asyncio
620  * Setup packet for control transfers. */
621 struct libusb_control_setup {
622         /** Request type. Bits 0:4 determine recipient, see
623          * \ref libusb_request_recipient. Bits 5:6 determine type, see
624          * \ref libusb_request_type. Bit 7 determines data transfer direction, see
625          * \ref libusb_endpoint_direction.
626          */
627         uint8_t  bmRequestType;
628
629         /** Request. If the type bits of bmRequestType are equal to
630          * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
631          * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
632          * \ref libusb_standard_request. For other cases, use of this field is
633          * application-specific. */
634         uint8_t  bRequest;
635
636         /** Value. Varies according to request */
637         uint16_t wValue;
638
639         /** Index. Varies according to request, typically used to pass an index
640          * or offset */
641         uint16_t wIndex;
642
643         /** Number of bytes to transfer */
644         uint16_t wLength;
645 };
646
647 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
648
649 /* libusbx */
650
651 struct libusb_context;
652 struct libusb_device;
653 struct libusb_device_handle;
654
655 /** \ingroup lib
656  * Structure providing the version of the libusbx runtime
657  */
658 struct libusb_version {
659         /** Library major version. */
660         const uint16_t major;
661
662         /** Library minor version. */
663         const uint16_t minor;
664
665         /** Library micro version. */
666         const uint16_t micro;
667
668         /** Library nano version. */
669         const uint16_t nano;
670
671         /** Library release candidate suffix string, e.g. "-rc4". */
672         const char *rc;
673
674         /** For ABI compatibility only. */
675         const char* describe;
676 };
677
678 /** \ingroup lib
679  * Structure representing a libusbx session. The concept of individual libusbx
680  * sessions allows for your program to use two libraries (or dynamically
681  * load two modules) which both independently use libusb. This will prevent
682  * interference between the individual libusbx users - for example
683  * libusb_set_debug() will not affect the other user of the library, and
684  * libusb_exit() will not destroy resources that the other user is still
685  * using.
686  *
687  * Sessions are created by libusb_init() and destroyed through libusb_exit().
688  * If your application is guaranteed to only ever include a single libusbx
689  * user (i.e. you), you do not have to worry about contexts: pass NULL in
690  * every function call where a context is required. The default context
691  * will be used.
692  *
693  * For more information, see \ref contexts.
694  */
695 typedef struct libusb_context libusb_context;
696
697 /** \ingroup dev
698  * Structure representing a USB device detected on the system. This is an
699  * opaque type for which you are only ever provided with a pointer, usually
700  * originating from libusb_get_device_list().
701  *
702  * Certain operations can be performed on a device, but in order to do any
703  * I/O you will have to first obtain a device handle using libusb_open().
704  *
705  * Devices are reference counted with libusb_device_ref() and
706  * libusb_device_unref(), and are freed when the reference count reaches 0.
707  * New devices presented by libusb_get_device_list() have a reference count of
708  * 1, and libusb_free_device_list() can optionally decrease the reference count
709  * on all devices in the list. libusb_open() adds another reference which is
710  * later destroyed by libusb_close().
711  */
712 typedef struct libusb_device libusb_device;
713
714
715 /** \ingroup dev
716  * Structure representing a handle on a USB device. This is an opaque type for
717  * which you are only ever provided with a pointer, usually originating from
718  * libusb_open().
719  *
720  * A device handle is used to perform I/O and other operations. When finished
721  * with a device handle, you should call libusb_close().
722  */
723 typedef struct libusb_device_handle libusb_device_handle;
724
725 /** \ingroup dev
726  * Speed codes. Indicates the speed at which the device is operating.
727  */
728 enum libusb_speed {
729         /** The OS doesn't report or know the device speed. */
730         LIBUSB_SPEED_UNKNOWN = 0,
731
732         /** The device is operating at low speed (1.5MBit/s). */
733         LIBUSB_SPEED_LOW = 1,
734
735         /** The device is operating at full speed (12MBit/s). */
736         LIBUSB_SPEED_FULL = 2,
737
738         /** The device is operating at high speed (480MBit/s). */
739         LIBUSB_SPEED_HIGH = 3,
740
741         /** The device is operating at super speed (5000MBit/s). */
742         LIBUSB_SPEED_SUPER = 4,
743 };
744
745 /** \ingroup misc
746  * Error codes. Most libusbx functions return 0 on success or one of these
747  * codes on failure.
748  * You can call \ref libusb_error_name() to retrieve a string representation
749  * of an error code.
750  */
751 enum libusb_error {
752         /** Success (no error) */
753         LIBUSB_SUCCESS = 0,
754
755         /** Input/output error */
756         LIBUSB_ERROR_IO = -1,
757
758         /** Invalid parameter */
759         LIBUSB_ERROR_INVALID_PARAM = -2,
760
761         /** Access denied (insufficient permissions) */
762         LIBUSB_ERROR_ACCESS = -3,
763
764         /** No such device (it may have been disconnected) */
765         LIBUSB_ERROR_NO_DEVICE = -4,
766
767         /** Entity not found */
768         LIBUSB_ERROR_NOT_FOUND = -5,
769
770         /** Resource busy */
771         LIBUSB_ERROR_BUSY = -6,
772
773         /** Operation timed out */
774         LIBUSB_ERROR_TIMEOUT = -7,
775
776         /** Overflow */
777         LIBUSB_ERROR_OVERFLOW = -8,
778
779         /** Pipe error */
780         LIBUSB_ERROR_PIPE = -9,
781
782         /** System call interrupted (perhaps due to signal) */
783         LIBUSB_ERROR_INTERRUPTED = -10,
784
785         /** Insufficient memory */
786         LIBUSB_ERROR_NO_MEM = -11,
787
788         /** Operation not supported or unimplemented on this platform */
789         LIBUSB_ERROR_NOT_SUPPORTED = -12,
790
791         /* NB! Remember to update libusb_error_name()
792            when adding new error codes here. */
793
794         /** Other error */
795         LIBUSB_ERROR_OTHER = -99,
796 };
797
798 /** \ingroup asyncio
799  * Transfer status codes */
800 enum libusb_transfer_status {
801         /** Transfer completed without error. Note that this does not indicate
802          * that the entire amount of requested data was transferred. */
803         LIBUSB_TRANSFER_COMPLETED,
804
805         /** Transfer failed */
806         LIBUSB_TRANSFER_ERROR,
807
808         /** Transfer timed out */
809         LIBUSB_TRANSFER_TIMED_OUT,
810
811         /** Transfer was cancelled */
812         LIBUSB_TRANSFER_CANCELLED,
813
814         /** For bulk/interrupt endpoints: halt condition detected (endpoint
815          * stalled). For control endpoints: control request not supported. */
816         LIBUSB_TRANSFER_STALL,
817
818         /** Device was disconnected */
819         LIBUSB_TRANSFER_NO_DEVICE,
820
821         /** Device sent more data than requested */
822         LIBUSB_TRANSFER_OVERFLOW,
823
824         /* NB! Remember to update libusb_error_name()
825            when adding new status codes here. */
826 };
827
828 /** \ingroup asyncio
829  * libusb_transfer.flags values */
830 enum libusb_transfer_flags {
831         /** Report short frames as errors */
832         LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
833
834         /** Automatically free() transfer buffer during libusb_free_transfer() */
835         LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
836
837         /** Automatically call libusb_free_transfer() after callback returns.
838          * If this flag is set, it is illegal to call libusb_free_transfer()
839          * from your transfer callback, as this will result in a double-free
840          * when this flag is acted upon. */
841         LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
842
843         /** Terminate transfers that are a multiple of the endpoint's
844          * wMaxPacketSize with an extra zero length packet. This is useful
845          * when a device protocol mandates that each logical request is
846          * terminated by an incomplete packet (i.e. the logical requests are
847          * not separated by other means).
848          *
849          * This flag only affects host-to-device transfers to bulk and interrupt
850          * endpoints. In other situations, it is ignored.
851          *
852          * This flag only affects transfers with a length that is a multiple of
853          * the endpoint's wMaxPacketSize. On transfers of other lengths, this
854          * flag has no effect. Therefore, if you are working with a device that
855          * needs a ZLP whenever the end of the logical request falls on a packet
856          * boundary, then it is sensible to set this flag on <em>every</em>
857          * transfer (you do not have to worry about only setting it on transfers
858          * that end on the boundary).
859          *
860          * This flag is currently only supported on Linux.
861          * On other systems, libusb_submit_transfer() will return
862          * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
863          *
864          * Available since libusb-1.0.9.
865          */
866         LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
867 };
868
869 /** \ingroup asyncio
870  * Isochronous packet descriptor. */
871 struct libusb_iso_packet_descriptor {
872         /** Length of data to request in this packet */
873         unsigned int length;
874
875         /** Amount of data that was actually transferred */
876         unsigned int actual_length;
877
878         /** Status code for this packet */
879         enum libusb_transfer_status status;
880 };
881
882 struct libusb_transfer;
883
884 /** \ingroup asyncio
885  * Asynchronous transfer callback function type. When submitting asynchronous
886  * transfers, you pass a pointer to a callback function of this type via the
887  * \ref libusb_transfer::callback "callback" member of the libusb_transfer
888  * structure. libusbx will call this function later, when the transfer has
889  * completed or failed. See \ref asyncio for more information.
890  * \param transfer The libusb_transfer struct the callback function is being
891  * notified about.
892  */
893 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
894
895 /** \ingroup asyncio
896  * The generic USB transfer structure. The user populates this structure and
897  * then submits it in order to request a transfer. After the transfer has
898  * completed, the library populates the transfer with the results and passes
899  * it back to the user.
900  */
901 struct libusb_transfer {
902         /** Handle of the device that this transfer will be submitted to */
903         libusb_device_handle *dev_handle;
904
905         /** A bitwise OR combination of \ref libusb_transfer_flags. */
906         uint8_t flags;
907
908         /** Address of the endpoint where this transfer will be sent. */
909         unsigned char endpoint;
910
911         /** Type of the endpoint from \ref libusb_transfer_type */
912         unsigned char type;
913
914         /** Timeout for this transfer in millseconds. A value of 0 indicates no
915          * timeout. */
916         unsigned int timeout;
917
918         /** The status of the transfer. Read-only, and only for use within
919          * transfer callback function.
920          *
921          * If this is an isochronous transfer, this field may read COMPLETED even
922          * if there were errors in the frames. Use the
923          * \ref libusb_iso_packet_descriptor::status "status" field in each packet
924          * to determine if errors occurred. */
925         enum libusb_transfer_status status;
926
927         /** Length of the data buffer */
928         int length;
929
930         /** Actual length of data that was transferred. Read-only, and only for
931          * use within transfer callback function. Not valid for isochronous
932          * endpoint transfers. */
933         int actual_length;
934
935         /** Callback function. This will be invoked when the transfer completes,
936          * fails, or is cancelled. */
937         libusb_transfer_cb_fn callback;
938
939         /** User context data to pass to the callback function. */
940         void *user_data;
941
942         /** Data buffer */
943         unsigned char *buffer;
944
945         /** Number of isochronous packets. Only used for I/O with isochronous
946          * endpoints. */
947         int num_iso_packets;
948
949         /** Isochronous packet descriptors, for isochronous transfers only. */
950         struct libusb_iso_packet_descriptor iso_packet_desc
951 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
952         [] /* valid C99 code */
953 #else
954         [0] /* non-standard, but usually working code */
955 #endif
956         ;
957 };
958
959 /** \ingroup misc
960  * Capabilities supported by this instance of libusb. Test if the loaded
961  * library supports a given capability by calling
962  * \ref libusb_has_capability().
963  */
964 enum libusb_capability {
965         /** The libusb_has_capability() API is available. */
966         LIBUSB_CAP_HAS_CAPABILITY = 0,
967 };
968
969 /** \ingroup lib
970  *  Log message levels.
971  *  - LIBUSB_LOG_LEVEL_NONE (0)    : no messages ever printed by the library (default)
972  *  - LIBUSB_LOG_LEVEL_ERROR (1)   : error messages are printed to stderr
973  *  - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
974  *  - LIBUSB_LOG_LEVEL_INFO (3)    : informational messages are printed to stdout, warning
975  *    and error messages are printed to stderr
976  *  - LIBUSB_LOG_LEVEL_DEBUG (4)   : debug and informational messages are printed to stdout,
977  *    warnings and errors to stderr
978  */
979 enum libusb_log_level {
980         LIBUSB_LOG_LEVEL_NONE = 0,
981         LIBUSB_LOG_LEVEL_ERROR,
982         LIBUSB_LOG_LEVEL_WARNING,
983         LIBUSB_LOG_LEVEL_INFO,
984         LIBUSB_LOG_LEVEL_DEBUG,
985 };
986
987 int LIBUSB_CALL libusb_init(libusb_context **ctx);
988 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
989 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
990 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
991 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
992 const char * LIBUSB_CALL libusb_error_name(int errcode);
993
994 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
995         libusb_device ***list);
996 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
997         int unref_devices);
998 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
999 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1000
1001 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1002         int *config);
1003 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1004         struct libusb_device_descriptor *desc);
1005 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1006         struct libusb_config_descriptor **config);
1007 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1008         uint8_t config_index, struct libusb_config_descriptor **config);
1009 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1010         uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1011 void LIBUSB_CALL libusb_free_config_descriptor(
1012         struct libusb_config_descriptor *config);
1013 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1014 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1015 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1016 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1017 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1018 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1019 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1020         unsigned char endpoint);
1021 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1022         unsigned char endpoint);
1023
1024 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
1025 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1026 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1027
1028 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
1029         int configuration);
1030 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
1031         int interface_number);
1032 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
1033         int interface_number);
1034
1035 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1036         libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1037
1038 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
1039         int interface_number, int alternate_setting);
1040 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
1041         unsigned char endpoint);
1042 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
1043
1044 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
1045         int interface_number);
1046 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
1047         int interface_number);
1048 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
1049         int interface_number);
1050
1051 /* async I/O */
1052
1053 /** \ingroup asyncio
1054  * Get the data section of a control transfer. This convenience function is here
1055  * to remind you that the data does not start until 8 bytes into the actual
1056  * buffer, as the setup packet comes first.
1057  *
1058  * Calling this function only makes sense from a transfer callback function,
1059  * or situations where you have already allocated a suitably sized buffer at
1060  * transfer->buffer.
1061  *
1062  * \param transfer a transfer
1063  * \returns pointer to the first byte of the data section
1064  */
1065 static inline unsigned char *libusb_control_transfer_get_data(
1066         struct libusb_transfer *transfer)
1067 {
1068         return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1069 }
1070
1071 /** \ingroup asyncio
1072  * Get the control setup packet of a control transfer. This convenience
1073  * function is here to remind you that the control setup occupies the first
1074  * 8 bytes of the transfer data buffer.
1075  *
1076  * Calling this function only makes sense from a transfer callback function,
1077  * or situations where you have already allocated a suitably sized buffer at
1078  * transfer->buffer.
1079  *
1080  * \param transfer a transfer
1081  * \returns a casted pointer to the start of the transfer data buffer
1082  */
1083 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1084         struct libusb_transfer *transfer)
1085 {
1086         return (struct libusb_control_setup *) transfer->buffer;
1087 }
1088
1089 /** \ingroup asyncio
1090  * Helper function to populate the setup packet (first 8 bytes of the data
1091  * buffer) for a control transfer. The wIndex, wValue and wLength values should
1092  * be given in host-endian byte order.
1093  *
1094  * \param buffer buffer to output the setup packet into
1095  * \param bmRequestType see the
1096  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1097  * \ref libusb_control_setup
1098  * \param bRequest see the
1099  * \ref libusb_control_setup::bRequest "bRequest" field of
1100  * \ref libusb_control_setup
1101  * \param wValue see the
1102  * \ref libusb_control_setup::wValue "wValue" field of
1103  * \ref libusb_control_setup
1104  * \param wIndex see the
1105  * \ref libusb_control_setup::wIndex "wIndex" field of
1106  * \ref libusb_control_setup
1107  * \param wLength see the
1108  * \ref libusb_control_setup::wLength "wLength" field of
1109  * \ref libusb_control_setup
1110  */
1111 static inline void libusb_fill_control_setup(unsigned char *buffer,
1112         uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1113         uint16_t wLength)
1114 {
1115         struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1116         setup->bmRequestType = bmRequestType;
1117         setup->bRequest = bRequest;
1118         setup->wValue = libusb_cpu_to_le16(wValue);
1119         setup->wIndex = libusb_cpu_to_le16(wIndex);
1120         setup->wLength = libusb_cpu_to_le16(wLength);
1121 }
1122
1123 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1124 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1125 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1126 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1127
1128 /** \ingroup asyncio
1129  * Helper function to populate the required \ref libusb_transfer fields
1130  * for a control transfer.
1131  *
1132  * If you pass a transfer buffer to this function, the first 8 bytes will
1133  * be interpreted as a control setup packet, and the wLength field will be
1134  * used to automatically populate the \ref libusb_transfer::length "length"
1135  * field of the transfer. Therefore the recommended approach is:
1136  * -# Allocate a suitably sized data buffer (including space for control setup)
1137  * -# Call libusb_fill_control_setup()
1138  * -# If this is a host-to-device transfer with a data stage, put the data
1139  *    in place after the setup packet
1140  * -# Call this function
1141  * -# Call libusb_submit_transfer()
1142  *
1143  * It is also legal to pass a NULL buffer to this function, in which case this
1144  * function will not attempt to populate the length field. Remember that you
1145  * must then populate the buffer and length fields later.
1146  *
1147  * \param transfer the transfer to populate
1148  * \param dev_handle handle of the device that will handle the transfer
1149  * \param buffer data buffer. If provided, this function will interpret the
1150  * first 8 bytes as a setup packet and infer the transfer length from that.
1151  * \param callback callback function to be invoked on transfer completion
1152  * \param user_data user data to pass to callback function
1153  * \param timeout timeout for the transfer in milliseconds
1154  */
1155 static inline void libusb_fill_control_transfer(
1156         struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1157         unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1158         unsigned int timeout)
1159 {
1160         struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1161         transfer->dev_handle = dev_handle;
1162         transfer->endpoint = 0;
1163         transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1164         transfer->timeout = timeout;
1165         transfer->buffer = buffer;
1166         if (setup)
1167                 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1168                         + libusb_le16_to_cpu(setup->wLength);
1169         transfer->user_data = user_data;
1170         transfer->callback = callback;
1171 }
1172
1173 /** \ingroup asyncio
1174  * Helper function to populate the required \ref libusb_transfer fields
1175  * for a bulk transfer.
1176  *
1177  * \param transfer the transfer to populate
1178  * \param dev_handle handle of the device that will handle the transfer
1179  * \param endpoint address of the endpoint where this transfer will be sent
1180  * \param buffer data buffer
1181  * \param length length of data buffer
1182  * \param callback callback function to be invoked on transfer completion
1183  * \param user_data user data to pass to callback function
1184  * \param timeout timeout for the transfer in milliseconds
1185  */
1186 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1187         libusb_device_handle *dev_handle, unsigned char endpoint,
1188         unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1189         void *user_data, unsigned int timeout)
1190 {
1191         transfer->dev_handle = dev_handle;
1192         transfer->endpoint = endpoint;
1193         transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1194         transfer->timeout = timeout;
1195         transfer->buffer = buffer;
1196         transfer->length = length;
1197         transfer->user_data = user_data;
1198         transfer->callback = callback;
1199 }
1200
1201 /** \ingroup asyncio
1202  * Helper function to populate the required \ref libusb_transfer fields
1203  * for an interrupt transfer.
1204  *
1205  * \param transfer the transfer to populate
1206  * \param dev_handle handle of the device that will handle the transfer
1207  * \param endpoint address of the endpoint where this transfer will be sent
1208  * \param buffer data buffer
1209  * \param length length of data buffer
1210  * \param callback callback function to be invoked on transfer completion
1211  * \param user_data user data to pass to callback function
1212  * \param timeout timeout for the transfer in milliseconds
1213  */
1214 static inline void libusb_fill_interrupt_transfer(
1215         struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1216         unsigned char endpoint, unsigned char *buffer, int length,
1217         libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1218 {
1219         transfer->dev_handle = dev_handle;
1220         transfer->endpoint = endpoint;
1221         transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1222         transfer->timeout = timeout;
1223         transfer->buffer = buffer;
1224         transfer->length = length;
1225         transfer->user_data = user_data;
1226         transfer->callback = callback;
1227 }
1228
1229 /** \ingroup asyncio
1230  * Helper function to populate the required \ref libusb_transfer fields
1231  * for an isochronous transfer.
1232  *
1233  * \param transfer the transfer to populate
1234  * \param dev_handle handle of the device that will handle the transfer
1235  * \param endpoint address of the endpoint where this transfer will be sent
1236  * \param buffer data buffer
1237  * \param length length of data buffer
1238  * \param num_iso_packets the number of isochronous packets
1239  * \param callback callback function to be invoked on transfer completion
1240  * \param user_data user data to pass to callback function
1241  * \param timeout timeout for the transfer in milliseconds
1242  */
1243 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1244         libusb_device_handle *dev_handle, unsigned char endpoint,
1245         unsigned char *buffer, int length, int num_iso_packets,
1246         libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1247 {
1248         transfer->dev_handle = dev_handle;
1249         transfer->endpoint = endpoint;
1250         transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1251         transfer->timeout = timeout;
1252         transfer->buffer = buffer;
1253         transfer->length = length;
1254         transfer->num_iso_packets = num_iso_packets;
1255         transfer->user_data = user_data;
1256         transfer->callback = callback;
1257 }
1258
1259 /** \ingroup asyncio
1260  * Convenience function to set the length of all packets in an isochronous
1261  * transfer, based on the num_iso_packets field in the transfer structure.
1262  *
1263  * \param transfer a transfer
1264  * \param length the length to set in each isochronous packet descriptor
1265  * \see libusb_get_max_packet_size()
1266  */
1267 static inline void libusb_set_iso_packet_lengths(
1268         struct libusb_transfer *transfer, unsigned int length)
1269 {
1270         int i;
1271         for (i = 0; i < transfer->num_iso_packets; i++)
1272                 transfer->iso_packet_desc[i].length = length;
1273 }
1274
1275 /** \ingroup asyncio
1276  * Convenience function to locate the position of an isochronous packet
1277  * within the buffer of an isochronous transfer.
1278  *
1279  * This is a thorough function which loops through all preceding packets,
1280  * accumulating their lengths to find the position of the specified packet.
1281  * Typically you will assign equal lengths to each packet in the transfer,
1282  * and hence the above method is sub-optimal. You may wish to use
1283  * libusb_get_iso_packet_buffer_simple() instead.
1284  *
1285  * \param transfer a transfer
1286  * \param packet the packet to return the address of
1287  * \returns the base address of the packet buffer inside the transfer buffer,
1288  * or NULL if the packet does not exist.
1289  * \see libusb_get_iso_packet_buffer_simple()
1290  */
1291 static inline unsigned char *libusb_get_iso_packet_buffer(
1292         struct libusb_transfer *transfer, unsigned int packet)
1293 {
1294         int i;
1295         size_t offset = 0;
1296         int _packet;
1297
1298         /* oops..slight bug in the API. packet is an unsigned int, but we use
1299          * signed integers almost everywhere else. range-check and convert to
1300          * signed to avoid compiler warnings. FIXME for libusb-2. */
1301         if (packet > INT_MAX)
1302                 return NULL;
1303         _packet = packet;
1304
1305         if (_packet >= transfer->num_iso_packets)
1306                 return NULL;
1307
1308         for (i = 0; i < _packet; i++)
1309                 offset += transfer->iso_packet_desc[i].length;
1310
1311         return transfer->buffer + offset;
1312 }
1313
1314 /** \ingroup asyncio
1315  * Convenience function to locate the position of an isochronous packet
1316  * within the buffer of an isochronous transfer, for transfers where each
1317  * packet is of identical size.
1318  *
1319  * This function relies on the assumption that every packet within the transfer
1320  * is of identical size to the first packet. Calculating the location of
1321  * the packet buffer is then just a simple calculation:
1322  * <tt>buffer + (packet_size * packet)</tt>
1323  *
1324  * Do not use this function on transfers other than those that have identical
1325  * packet lengths for each packet.
1326  *
1327  * \param transfer a transfer
1328  * \param packet the packet to return the address of
1329  * \returns the base address of the packet buffer inside the transfer buffer,
1330  * or NULL if the packet does not exist.
1331  * \see libusb_get_iso_packet_buffer()
1332  */
1333 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1334         struct libusb_transfer *transfer, unsigned int packet)
1335 {
1336         int _packet;
1337
1338         /* oops..slight bug in the API. packet is an unsigned int, but we use
1339          * signed integers almost everywhere else. range-check and convert to
1340          * signed to avoid compiler warnings. FIXME for libusb-2. */
1341         if (packet > INT_MAX)
1342                 return NULL;
1343         _packet = packet;
1344
1345         if (_packet >= transfer->num_iso_packets)
1346                 return NULL;
1347
1348         return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1349 }
1350
1351 /* sync I/O */
1352
1353 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1354         uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1355         unsigned char *data, uint16_t wLength, unsigned int timeout);
1356
1357 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1358         unsigned char endpoint, unsigned char *data, int length,
1359         int *actual_length, unsigned int timeout);
1360
1361 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1362         unsigned char endpoint, unsigned char *data, int length,
1363         int *actual_length, unsigned int timeout);
1364
1365 /** \ingroup desc
1366  * Retrieve a descriptor from the default control pipe.
1367  * This is a convenience function which formulates the appropriate control
1368  * message to retrieve the descriptor.
1369  *
1370  * \param dev a device handle
1371  * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1372  * \param desc_index the index of the descriptor to retrieve
1373  * \param data output buffer for descriptor
1374  * \param length size of data buffer
1375  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1376  */
1377 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1378         uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1379 {
1380         return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1381                 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1382                 (uint16_t) length, 1000);
1383 }
1384
1385 /** \ingroup desc
1386  * Retrieve a descriptor from a device.
1387  * This is a convenience function which formulates the appropriate control
1388  * message to retrieve the descriptor. The string returned is Unicode, as
1389  * detailed in the USB specifications.
1390  *
1391  * \param dev a device handle
1392  * \param desc_index the index of the descriptor to retrieve
1393  * \param langid the language ID for the string descriptor
1394  * \param data output buffer for descriptor
1395  * \param length size of data buffer
1396  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1397  * \see libusb_get_string_descriptor_ascii()
1398  */
1399 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1400         uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1401 {
1402         return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1403                 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1404                 langid, data, (uint16_t) length, 1000);
1405 }
1406
1407 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1408         uint8_t desc_index, unsigned char *data, int length);
1409
1410 /* polling and timeouts */
1411
1412 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1413 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1414 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1415 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1416 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1417 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1418 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1419 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1420
1421 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1422         struct timeval *tv);
1423 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1424         struct timeval *tv, int *completed);
1425 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1426 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1427 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1428         struct timeval *tv);
1429 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1430 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1431         struct timeval *tv);
1432
1433 /** \ingroup poll
1434  * File descriptor for polling
1435  */
1436 struct libusb_pollfd {
1437         /** Numeric file descriptor */
1438         int fd;
1439
1440         /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1441          * should monitor this file descriptor for becoming ready to read from,
1442          * and POLLOUT indicates that you should monitor this file descriptor for
1443          * nonblocking write readiness. */
1444         short events;
1445 };
1446
1447 /** \ingroup poll
1448  * Callback function, invoked when a new file descriptor should be added
1449  * to the set of file descriptors monitored for events.
1450  * \param fd the new file descriptor
1451  * \param events events to monitor for, see \ref libusb_pollfd for a
1452  * description
1453  * \param user_data User data pointer specified in
1454  * libusb_set_pollfd_notifiers() call
1455  * \see libusb_set_pollfd_notifiers()
1456  */
1457 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1458         void *user_data);
1459
1460 /** \ingroup poll
1461  * Callback function, invoked when a file descriptor should be removed from
1462  * the set of file descriptors being monitored for events. After returning
1463  * from this callback, do not use that file descriptor again.
1464  * \param fd the file descriptor to stop monitoring
1465  * \param user_data User data pointer specified in
1466  * libusb_set_pollfd_notifiers() call
1467  * \see libusb_set_pollfd_notifiers()
1468  */
1469 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1470
1471 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1472         libusb_context *ctx);
1473 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1474         libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1475         void *user_data);
1476
1477 #ifdef __cplusplus
1478 }
1479 #endif
1480
1481 #endif