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