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