2 * Public libusbx header file
3 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
4 * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
5 * Copyright © 2012 Pete Batard <pete@akeo.ie>
6 * Copyright © 2012 Nathan Hjelm <hjelmn@cs.unm.edu>
7 * For more information, please visit: http://libusbx.org
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 /* on MS environments, the inline keyword is available in C++ only */
29 #if !defined(__cplusplus)
30 #define inline __inline
32 /* ssize_t is also not available (copy/paste from MinGW) */
33 #ifndef _SSIZE_T_DEFINED
34 #define _SSIZE_T_DEFINED
37 typedef __int64 ssize_t;
41 #endif /* _SSIZE_T_DEFINED */
44 /* stdint.h is not available on older MSVC */
45 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
46 typedef unsigned __int8 uint8_t;
47 typedef unsigned __int16 uint16_t;
48 typedef unsigned __int32 uint32_t;
53 #if !defined(_WIN32_WCE)
54 #include <sys/types.h>
57 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
64 /* 'interface' might be defined as a macro on Windows, so we need to
65 * undefine it so as not to break the current libusbx API, because
66 * libusb_config_descriptor has an 'interface' member
67 * As this can be problematic if you include windows.h after libusb.h
68 * in your sources, we force windows.h to be included first. */
69 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
71 #if defined(interface)
74 #if !defined(__CYGWIN__)
79 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
80 #define LIBUSB_DEPRECATED_FOR(f) \
81 __attribute__((deprecated("Use " #f " instead")))
83 #define LIBUSB_DEPRECATED_FOR(f)
88 * libusbx's Windows calling convention.
90 * Under Windows, the selection of available compilers and configurations
91 * means that, unlike other platforms, there is not <em>one true calling
92 * convention</em> (calling convention: the manner in which parameters are
93 * passed to funcions in the generated assembly code).
95 * Matching the Windows API itself, libusbx uses the WINAPI convention (which
96 * translates to the <tt>stdcall</tt> convention) and guarantees that the
97 * library is compiled in this way. The public header file also includes
98 * appropriate annotations so that your own software will use the right
99 * convention, even if another convention is being used by default within
102 * The one consideration that you must apply in your software is to mark
103 * all functions which you use as libusbx callbacks with this LIBUSB_CALL
104 * annotation, so that they too get compiled for the correct calling
107 * On non-Windows operating systems, this macro is defined as nothing. This
108 * means that you can apply it to your code without worrying about
109 * cross-platform compatibility.
111 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx
112 * functions. You'd think that declaration would be enough, but cygwin will
113 * complain about conflicting types unless both are marked this way.
114 * The placement of this macro is important too; it must appear after the
115 * return type, before the function name. See internal documentation for
118 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
119 #define LIBUSB_CALL WINAPI
124 /** \def LIBUSBX_API_VERSION
126 * libusbx's API version.
128 * Since version 1.0.13, to help with feature detection, libusbx defines
129 * a LIBUSBX_API_VERSION macro that gets increased every time there is a
130 * significant change to the API, such as the introduction of a new call,
131 * the definition of a new macro/enum member, or any other element that
132 * libusbx applications may want to detect at compilation time.
134 * The macro is typically used in an application as follows:
136 * #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01001234)
137 * // Use one of the newer features from the libusbx API
141 * Another feature of LIBUSBX_API_VERSION is that it can be used to detect
142 * whether you are compiling against the libusb or the libusbx library.
144 * Internally, LIBUSBX_API_VERSION is defined as follows:
145 * (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental)
147 #define LIBUSBX_API_VERSION 0x01000102
153 /** \def libusb_cpu_to_le16
155 * Convert a 16-bit value from host-endian to little-endian format. On
156 * little endian systems, this function does nothing. On big endian systems,
157 * the bytes are swapped.
158 * \param x the host-endian value to convert
159 * \returns the value in little-endian byte order
161 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
168 _tmp.b8[0] = x & 0xff;
172 /** \def libusb_le16_to_cpu
174 * Convert a 16-bit value from little-endian to host-endian format. On
175 * little endian systems, this function does nothing. On big endian systems,
176 * the bytes are swapped.
177 * \param x the little-endian value to convert
178 * \returns the value in host-endian byte order
180 #define libusb_le16_to_cpu libusb_cpu_to_le16
182 /* standard USB stuff */
185 * Device and/or Interface Class codes */
186 enum libusb_class_code {
187 /** In the context of a \ref libusb_device_descriptor "device descriptor",
188 * this bDeviceClass value indicates that each interface specifies its
189 * own class information and all interfaces operate independently.
191 LIBUSB_CLASS_PER_INTERFACE = 0,
194 LIBUSB_CLASS_AUDIO = 1,
196 /** Communications class */
197 LIBUSB_CLASS_COMM = 2,
199 /** Human Interface Device class */
200 LIBUSB_CLASS_HID = 3,
203 LIBUSB_CLASS_PHYSICAL = 5,
206 LIBUSB_CLASS_PRINTER = 7,
209 LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
210 LIBUSB_CLASS_IMAGE = 6,
212 /** Mass storage class */
213 LIBUSB_CLASS_MASS_STORAGE = 8,
216 LIBUSB_CLASS_HUB = 9,
219 LIBUSB_CLASS_DATA = 10,
222 LIBUSB_CLASS_SMART_CARD = 0x0b,
224 /** Content Security */
225 LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
228 LIBUSB_CLASS_VIDEO = 0x0e,
230 /** Personal Healthcare */
231 LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
233 /** Diagnostic Device */
234 LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
236 /** Wireless class */
237 LIBUSB_CLASS_WIRELESS = 0xe0,
239 /** Application class */
240 LIBUSB_CLASS_APPLICATION = 0xfe,
242 /** Class is vendor-specific */
243 LIBUSB_CLASS_VENDOR_SPEC = 0xff
247 * Descriptor types as defined by the USB specification. */
248 enum libusb_descriptor_type {
249 /** Device descriptor. See libusb_device_descriptor. */
250 LIBUSB_DT_DEVICE = 0x01,
252 /** Configuration descriptor. See libusb_config_descriptor. */
253 LIBUSB_DT_CONFIG = 0x02,
255 /** String descriptor */
256 LIBUSB_DT_STRING = 0x03,
258 /** Interface descriptor. See libusb_interface_descriptor. */
259 LIBUSB_DT_INTERFACE = 0x04,
261 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
262 LIBUSB_DT_ENDPOINT = 0x05,
264 /** HID descriptor */
265 LIBUSB_DT_HID = 0x21,
267 /** HID report descriptor */
268 LIBUSB_DT_REPORT = 0x22,
270 /** Physical descriptor */
271 LIBUSB_DT_PHYSICAL = 0x23,
273 /** Hub descriptor */
274 LIBUSB_DT_HUB = 0x29,
276 /** SuperSpeed Hub descriptor */
277 LIBUSB_DT_SUPERSPEED_HUB = 0x2A,
280 /* Descriptor sizes per descriptor type */
281 #define LIBUSB_DT_DEVICE_SIZE 18
282 #define LIBUSB_DT_CONFIG_SIZE 9
283 #define LIBUSB_DT_INTERFACE_SIZE 9
284 #define LIBUSB_DT_ENDPOINT_SIZE 7
285 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
286 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
288 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
289 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
292 * Endpoint direction. Values for bit 7 of the
293 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
295 enum libusb_endpoint_direction {
296 /** In: device-to-host */
297 LIBUSB_ENDPOINT_IN = 0x80,
299 /** Out: host-to-device */
300 LIBUSB_ENDPOINT_OUT = 0x00
303 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
306 * Endpoint transfer type. Values for bits 0:1 of the
307 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
309 enum libusb_transfer_type {
310 /** Control endpoint */
311 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
313 /** Isochronous endpoint */
314 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
317 LIBUSB_TRANSFER_TYPE_BULK = 2,
319 /** Interrupt endpoint */
320 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
324 * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
325 enum libusb_standard_request {
326 /** Request status of the specific recipient */
327 LIBUSB_REQUEST_GET_STATUS = 0x00,
329 /** Clear or disable a specific feature */
330 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
332 /* 0x02 is reserved */
334 /** Set or enable a specific feature */
335 LIBUSB_REQUEST_SET_FEATURE = 0x03,
337 /* 0x04 is reserved */
339 /** Set device address for all future accesses */
340 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
342 /** Get the specified descriptor */
343 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
345 /** Used to update existing descriptors or add new descriptors */
346 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
348 /** Get the current device configuration value */
349 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
351 /** Set device configuration */
352 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
354 /** Return the selected alternate setting for the specified interface */
355 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
357 /** Select an alternate interface for the specified interface */
358 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
360 /** Set then report an endpoint's synchronization frame */
361 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
363 /** Sets both the U1 and U2 Exit Latency */
364 LIBUSB_REQUEST_SET_SEL = 0x30,
366 /** Delay from the time a host transmits a packet to the time it is
367 * received by the device. */
368 LIBUSB_SET_ISOCH_DELAY = 0x31,
372 * Request type bits of the
373 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
375 enum libusb_request_type {
377 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
380 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
383 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
386 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
390 * Recipient bits of the
391 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
392 * transfers. Values 4 through 31 are reserved. */
393 enum libusb_request_recipient {
395 LIBUSB_RECIPIENT_DEVICE = 0x00,
398 LIBUSB_RECIPIENT_INTERFACE = 0x01,
401 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
404 LIBUSB_RECIPIENT_OTHER = 0x03,
407 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
410 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
411 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
412 * libusb_endpoint_descriptor.
414 enum libusb_iso_sync_type {
415 /** No synchronization */
416 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
419 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
422 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
425 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
428 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
431 * Usage type for isochronous endpoints. Values for bits 4:5 of the
432 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
433 * libusb_endpoint_descriptor.
435 enum libusb_iso_usage_type {
437 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
439 /** Feedback endpoint */
440 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
442 /** Implicit feedback Data endpoint */
443 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
447 * A structure representing the standard USB device descriptor. This
448 * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
449 * All multiple-byte fields are represented in host-endian format.
451 struct libusb_device_descriptor {
452 /** Size of this descriptor (in bytes) */
455 /** Descriptor type. Will have value
456 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
458 uint8_t bDescriptorType;
460 /** USB specification release number in binary-coded decimal. A value of
461 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
464 /** USB-IF class code for the device. See \ref libusb_class_code. */
465 uint8_t bDeviceClass;
467 /** USB-IF subclass code for the device, qualified by the bDeviceClass
469 uint8_t bDeviceSubClass;
471 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
472 * bDeviceSubClass values */
473 uint8_t bDeviceProtocol;
475 /** Maximum packet size for endpoint 0 */
476 uint8_t bMaxPacketSize0;
478 /** USB-IF vendor ID */
481 /** USB-IF product ID */
484 /** Device release number in binary-coded decimal */
487 /** Index of string descriptor describing manufacturer */
488 uint8_t iManufacturer;
490 /** Index of string descriptor describing product */
493 /** Index of string descriptor containing device serial number */
494 uint8_t iSerialNumber;
496 /** Number of possible configurations */
497 uint8_t bNumConfigurations;
501 * A structure representing the standard USB endpoint descriptor. This
502 * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
503 * All multiple-byte fields are represented in host-endian format.
505 struct libusb_endpoint_descriptor {
506 /** Size of this descriptor (in bytes) */
509 /** Descriptor type. Will have value
510 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
512 uint8_t bDescriptorType;
514 /** The address of the endpoint described by this descriptor. Bits 0:3 are
515 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
516 * see \ref libusb_endpoint_direction.
518 uint8_t bEndpointAddress;
520 /** Attributes which apply to the endpoint when it is configured using
521 * the bConfigurationValue. Bits 0:1 determine the transfer type and
522 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
523 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
524 * Bits 4:5 are also only used for isochronous endpoints and correspond to
525 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
527 uint8_t bmAttributes;
529 /** Maximum packet size this endpoint is capable of sending/receiving. */
530 uint16_t wMaxPacketSize;
532 /** Interval for polling endpoint for data transfers. */
535 /** For audio devices only: the rate at which synchronization feedback
539 /** For audio devices only: the address if the synch endpoint */
540 uint8_t bSynchAddress;
542 /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
543 * it will store them here, should you wish to parse them. */
544 const unsigned char *extra;
546 /** Length of the extra descriptors, in bytes. */
551 * A structure representing the standard USB interface descriptor. This
552 * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
553 * All multiple-byte fields are represented in host-endian format.
555 struct libusb_interface_descriptor {
556 /** Size of this descriptor (in bytes) */
559 /** Descriptor type. Will have value
560 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
561 * in this context. */
562 uint8_t bDescriptorType;
564 /** Number of this interface */
565 uint8_t bInterfaceNumber;
567 /** Value used to select this alternate setting for this interface */
568 uint8_t bAlternateSetting;
570 /** Number of endpoints used by this interface (excluding the control
572 uint8_t bNumEndpoints;
574 /** USB-IF class code for this interface. See \ref libusb_class_code. */
575 uint8_t bInterfaceClass;
577 /** USB-IF subclass code for this interface, qualified by the
578 * bInterfaceClass value */
579 uint8_t bInterfaceSubClass;
581 /** USB-IF protocol code for this interface, qualified by the
582 * bInterfaceClass and bInterfaceSubClass values */
583 uint8_t bInterfaceProtocol;
585 /** Index of string descriptor describing this interface */
588 /** Array of endpoint descriptors. This length of this array is determined
589 * by the bNumEndpoints field. */
590 const struct libusb_endpoint_descriptor *endpoint;
592 /** Extra descriptors. If libusbx encounters unknown interface descriptors,
593 * it will store them here, should you wish to parse them. */
594 const unsigned char *extra;
596 /** Length of the extra descriptors, in bytes. */
601 * A collection of alternate settings for a particular USB interface.
603 struct libusb_interface {
604 /** Array of interface descriptors. The length of this array is determined
605 * by the num_altsetting field. */
606 const struct libusb_interface_descriptor *altsetting;
608 /** The number of alternate settings that belong to this interface */
613 * A structure representing the standard USB configuration descriptor. This
614 * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
615 * All multiple-byte fields are represented in host-endian format.
617 struct libusb_config_descriptor {
618 /** Size of this descriptor (in bytes) */
621 /** Descriptor type. Will have value
622 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
623 * in this context. */
624 uint8_t bDescriptorType;
626 /** Total length of data returned for this configuration */
627 uint16_t wTotalLength;
629 /** Number of interfaces supported by this configuration */
630 uint8_t bNumInterfaces;
632 /** Identifier value for this configuration */
633 uint8_t bConfigurationValue;
635 /** Index of string descriptor describing this configuration */
636 uint8_t iConfiguration;
638 /** Configuration characteristics */
639 uint8_t bmAttributes;
641 /** Maximum power consumption of the USB device from this bus in this
642 * configuration when the device is fully opreation. Expressed in units
646 /** Array of interfaces supported by this configuration. The length of
647 * this array is determined by the bNumInterfaces field. */
648 const struct libusb_interface *interface;
650 /** Extra descriptors. If libusbx encounters unknown configuration
651 * descriptors, it will store them here, should you wish to parse them. */
652 const unsigned char *extra;
654 /** Length of the extra descriptors, in bytes. */
659 * Setup packet for control transfers. */
660 struct libusb_control_setup {
661 /** Request type. Bits 0:4 determine recipient, see
662 * \ref libusb_request_recipient. Bits 5:6 determine type, see
663 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
664 * \ref libusb_endpoint_direction.
666 uint8_t bmRequestType;
668 /** Request. If the type bits of bmRequestType are equal to
669 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
670 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
671 * \ref libusb_standard_request. For other cases, use of this field is
672 * application-specific. */
675 /** Value. Varies according to request */
678 /** Index. Varies according to request, typically used to pass an index
682 /** Number of bytes to transfer */
686 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
690 struct libusb_context;
691 struct libusb_device;
692 struct libusb_device_handle;
693 struct libusb_hotplug_callback;
696 * Structure providing the version of the libusbx runtime
698 struct libusb_version {
699 /** Library major version. */
700 const uint16_t major;
702 /** Library minor version. */
703 const uint16_t minor;
705 /** Library micro version. */
706 const uint16_t micro;
708 /** Library nano version. */
711 /** Library release candidate suffix string, e.g. "-rc4". */
714 /** For ABI compatibility only. */
715 const char* describe;
719 * Structure representing a libusbx session. The concept of individual libusbx
720 * sessions allows for your program to use two libraries (or dynamically
721 * load two modules) which both independently use libusb. This will prevent
722 * interference between the individual libusbx users - for example
723 * libusb_set_debug() will not affect the other user of the library, and
724 * libusb_exit() will not destroy resources that the other user is still
727 * Sessions are created by libusb_init() and destroyed through libusb_exit().
728 * If your application is guaranteed to only ever include a single libusbx
729 * user (i.e. you), you do not have to worry about contexts: pass NULL in
730 * every function call where a context is required. The default context
733 * For more information, see \ref contexts.
735 typedef struct libusb_context libusb_context;
738 * Structure representing a USB device detected on the system. This is an
739 * opaque type for which you are only ever provided with a pointer, usually
740 * originating from libusb_get_device_list().
742 * Certain operations can be performed on a device, but in order to do any
743 * I/O you will have to first obtain a device handle using libusb_open().
745 * Devices are reference counted with libusb_ref_device() and
746 * libusb_unref_device(), and are freed when the reference count reaches 0.
747 * New devices presented by libusb_get_device_list() have a reference count of
748 * 1, and libusb_free_device_list() can optionally decrease the reference count
749 * on all devices in the list. libusb_open() adds another reference which is
750 * later destroyed by libusb_close().
752 typedef struct libusb_device libusb_device;
756 * Structure representing a handle on a USB device. This is an opaque type for
757 * which you are only ever provided with a pointer, usually originating from
760 * A device handle is used to perform I/O and other operations. When finished
761 * with a device handle, you should call libusb_close().
763 typedef struct libusb_device_handle libusb_device_handle;
766 * Speed codes. Indicates the speed at which the device is operating.
769 /** The OS doesn't report or know the device speed. */
770 LIBUSB_SPEED_UNKNOWN = 0,
772 /** The device is operating at low speed (1.5MBit/s). */
773 LIBUSB_SPEED_LOW = 1,
775 /** The device is operating at full speed (12MBit/s). */
776 LIBUSB_SPEED_FULL = 2,
778 /** The device is operating at high speed (480MBit/s). */
779 LIBUSB_SPEED_HIGH = 3,
781 /** The device is operating at super speed (5000MBit/s). */
782 LIBUSB_SPEED_SUPER = 4,
786 * Error codes. Most libusbx functions return 0 on success or one of these
788 * You can call \ref libusb_error_name() to retrieve a string representation
792 /** Success (no error) */
795 /** Input/output error */
796 LIBUSB_ERROR_IO = -1,
798 /** Invalid parameter */
799 LIBUSB_ERROR_INVALID_PARAM = -2,
801 /** Access denied (insufficient permissions) */
802 LIBUSB_ERROR_ACCESS = -3,
804 /** No such device (it may have been disconnected) */
805 LIBUSB_ERROR_NO_DEVICE = -4,
807 /** Entity not found */
808 LIBUSB_ERROR_NOT_FOUND = -5,
811 LIBUSB_ERROR_BUSY = -6,
813 /** Operation timed out */
814 LIBUSB_ERROR_TIMEOUT = -7,
817 LIBUSB_ERROR_OVERFLOW = -8,
820 LIBUSB_ERROR_PIPE = -9,
822 /** System call interrupted (perhaps due to signal) */
823 LIBUSB_ERROR_INTERRUPTED = -10,
825 /** Insufficient memory */
826 LIBUSB_ERROR_NO_MEM = -11,
828 /** Operation not supported or unimplemented on this platform */
829 LIBUSB_ERROR_NOT_SUPPORTED = -12,
831 /* NB! Remember to update libusb_error_name()
832 when adding new error codes here. */
835 LIBUSB_ERROR_OTHER = -99,
839 * Transfer status codes */
840 enum libusb_transfer_status {
841 /** Transfer completed without error. Note that this does not indicate
842 * that the entire amount of requested data was transferred. */
843 LIBUSB_TRANSFER_COMPLETED,
845 /** Transfer failed */
846 LIBUSB_TRANSFER_ERROR,
848 /** Transfer timed out */
849 LIBUSB_TRANSFER_TIMED_OUT,
851 /** Transfer was cancelled */
852 LIBUSB_TRANSFER_CANCELLED,
854 /** For bulk/interrupt endpoints: halt condition detected (endpoint
855 * stalled). For control endpoints: control request not supported. */
856 LIBUSB_TRANSFER_STALL,
858 /** Device was disconnected */
859 LIBUSB_TRANSFER_NO_DEVICE,
861 /** Device sent more data than requested */
862 LIBUSB_TRANSFER_OVERFLOW,
864 /* NB! Remember to update libusb_error_name()
865 when adding new status codes here. */
869 * libusb_transfer.flags values */
870 enum libusb_transfer_flags {
871 /** Report short frames as errors */
872 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
874 /** Automatically free() transfer buffer during libusb_free_transfer() */
875 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
877 /** Automatically call libusb_free_transfer() after callback returns.
878 * If this flag is set, it is illegal to call libusb_free_transfer()
879 * from your transfer callback, as this will result in a double-free
880 * when this flag is acted upon. */
881 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
883 /** Terminate transfers that are a multiple of the endpoint's
884 * wMaxPacketSize with an extra zero length packet. This is useful
885 * when a device protocol mandates that each logical request is
886 * terminated by an incomplete packet (i.e. the logical requests are
887 * not separated by other means).
889 * This flag only affects host-to-device transfers to bulk and interrupt
890 * endpoints. In other situations, it is ignored.
892 * This flag only affects transfers with a length that is a multiple of
893 * the endpoint's wMaxPacketSize. On transfers of other lengths, this
894 * flag has no effect. Therefore, if you are working with a device that
895 * needs a ZLP whenever the end of the logical request falls on a packet
896 * boundary, then it is sensible to set this flag on <em>every</em>
897 * transfer (you do not have to worry about only setting it on transfers
898 * that end on the boundary).
900 * This flag is currently only supported on Linux.
901 * On other systems, libusb_submit_transfer() will return
902 * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
904 * Available since libusb-1.0.9.
906 LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
910 * Isochronous packet descriptor. */
911 struct libusb_iso_packet_descriptor {
912 /** Length of data to request in this packet */
915 /** Amount of data that was actually transferred */
916 unsigned int actual_length;
918 /** Status code for this packet */
919 enum libusb_transfer_status status;
922 struct libusb_transfer;
925 * Asynchronous transfer callback function type. When submitting asynchronous
926 * transfers, you pass a pointer to a callback function of this type via the
927 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
928 * structure. libusbx will call this function later, when the transfer has
929 * completed or failed. See \ref asyncio for more information.
930 * \param transfer The libusb_transfer struct the callback function is being
933 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
936 * The generic USB transfer structure. The user populates this structure and
937 * then submits it in order to request a transfer. After the transfer has
938 * completed, the library populates the transfer with the results and passes
939 * it back to the user.
941 struct libusb_transfer {
942 /** Handle of the device that this transfer will be submitted to */
943 libusb_device_handle *dev_handle;
945 /** A bitwise OR combination of \ref libusb_transfer_flags. */
948 /** Address of the endpoint where this transfer will be sent. */
949 unsigned char endpoint;
951 /** Type of the endpoint from \ref libusb_transfer_type */
954 /** Timeout for this transfer in millseconds. A value of 0 indicates no
956 unsigned int timeout;
958 /** The status of the transfer. Read-only, and only for use within
959 * transfer callback function.
961 * If this is an isochronous transfer, this field may read COMPLETED even
962 * if there were errors in the frames. Use the
963 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
964 * to determine if errors occurred. */
965 enum libusb_transfer_status status;
967 /** Length of the data buffer */
970 /** Actual length of data that was transferred. Read-only, and only for
971 * use within transfer callback function. Not valid for isochronous
972 * endpoint transfers. */
975 /** Callback function. This will be invoked when the transfer completes,
976 * fails, or is cancelled. */
977 libusb_transfer_cb_fn callback;
979 /** User context data to pass to the callback function. */
983 unsigned char *buffer;
985 /** Number of isochronous packets. Only used for I/O with isochronous
989 /** Isochronous packet descriptors, for isochronous transfers only. */
990 struct libusb_iso_packet_descriptor iso_packet_desc
991 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
992 [] /* valid C99 code */
994 [0] /* non-standard, but usually working code */
1000 * Capabilities supported by this instance of libusb. Test if the loaded
1001 * library supports a given capability by calling
1002 * \ref libusb_has_capability().
1004 enum libusb_capability {
1005 /** The libusb_has_capability() API is available. */
1006 LIBUSB_CAP_HAS_CAPABILITY = 0x0000,
1007 /** Hotplug support is available. */
1008 LIBUSB_CAP_HAS_HOTPLUG = 0x0001,
1009 /** The library can access HID devices without requiring user intervention.
1010 * Note that before being able to actually access an HID device, you may
1011 * still have to call additional libusbx functions such as
1012 * \ref libusb_detach_kernel_driver(). */
1013 LIBUSB_CAP_HAS_HID_ACCESS = 0x0100,
1014 /** The library supports detaching of the default USB driver, using
1015 * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
1016 LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101
1020 * Log message levels.
1021 * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
1022 * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr
1023 * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
1024 * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning
1025 * and error messages are printed to stderr
1026 * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout,
1027 * warnings and errors to stderr
1029 enum libusb_log_level {
1030 LIBUSB_LOG_LEVEL_NONE = 0,
1031 LIBUSB_LOG_LEVEL_ERROR,
1032 LIBUSB_LOG_LEVEL_WARNING,
1033 LIBUSB_LOG_LEVEL_INFO,
1034 LIBUSB_LOG_LEVEL_DEBUG,
1037 int LIBUSB_CALL libusb_init(libusb_context **ctx);
1038 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
1039 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1040 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1041 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1042 const char * LIBUSB_CALL libusb_error_name(int errcode);
1044 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
1045 libusb_device ***list);
1046 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
1048 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
1049 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1051 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1053 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1054 struct libusb_device_descriptor *desc);
1055 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1056 struct libusb_config_descriptor **config);
1057 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1058 uint8_t config_index, struct libusb_config_descriptor **config);
1059 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1060 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1061 void LIBUSB_CALL libusb_free_config_descriptor(
1062 struct libusb_config_descriptor *config);
1063 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1064 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1065 int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len);
1066 LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers)
1067 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1068 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1069 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1070 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1071 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1072 unsigned char endpoint);
1073 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1074 unsigned char endpoint);
1076 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
1077 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1078 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1080 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
1082 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
1083 int interface_number);
1084 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
1085 int interface_number);
1087 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1088 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1090 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
1091 int interface_number, int alternate_setting);
1092 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
1093 unsigned char endpoint);
1094 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
1096 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
1097 int interface_number);
1098 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
1099 int interface_number);
1100 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
1101 int interface_number);
1105 /** \ingroup asyncio
1106 * Get the data section of a control transfer. This convenience function is here
1107 * to remind you that the data does not start until 8 bytes into the actual
1108 * buffer, as the setup packet comes first.
1110 * Calling this function only makes sense from a transfer callback function,
1111 * or situations where you have already allocated a suitably sized buffer at
1114 * \param transfer a transfer
1115 * \returns pointer to the first byte of the data section
1117 static inline unsigned char *libusb_control_transfer_get_data(
1118 struct libusb_transfer *transfer)
1120 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1123 /** \ingroup asyncio
1124 * Get the control setup packet of a control transfer. This convenience
1125 * function is here to remind you that the control setup occupies the first
1126 * 8 bytes of the transfer data buffer.
1128 * Calling this function only makes sense from a transfer callback function,
1129 * or situations where you have already allocated a suitably sized buffer at
1132 * \param transfer a transfer
1133 * \returns a casted pointer to the start of the transfer data buffer
1135 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1136 struct libusb_transfer *transfer)
1138 return (struct libusb_control_setup *) transfer->buffer;
1141 /** \ingroup asyncio
1142 * Helper function to populate the setup packet (first 8 bytes of the data
1143 * buffer) for a control transfer. The wIndex, wValue and wLength values should
1144 * be given in host-endian byte order.
1146 * \param buffer buffer to output the setup packet into
1147 * \param bmRequestType see the
1148 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1149 * \ref libusb_control_setup
1150 * \param bRequest see the
1151 * \ref libusb_control_setup::bRequest "bRequest" field of
1152 * \ref libusb_control_setup
1153 * \param wValue see the
1154 * \ref libusb_control_setup::wValue "wValue" field of
1155 * \ref libusb_control_setup
1156 * \param wIndex see the
1157 * \ref libusb_control_setup::wIndex "wIndex" field of
1158 * \ref libusb_control_setup
1159 * \param wLength see the
1160 * \ref libusb_control_setup::wLength "wLength" field of
1161 * \ref libusb_control_setup
1163 static inline void libusb_fill_control_setup(unsigned char *buffer,
1164 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1167 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1168 setup->bmRequestType = bmRequestType;
1169 setup->bRequest = bRequest;
1170 setup->wValue = libusb_cpu_to_le16(wValue);
1171 setup->wIndex = libusb_cpu_to_le16(wIndex);
1172 setup->wLength = libusb_cpu_to_le16(wLength);
1175 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1176 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1177 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1178 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1180 /** \ingroup asyncio
1181 * Helper function to populate the required \ref libusb_transfer fields
1182 * for a control transfer.
1184 * If you pass a transfer buffer to this function, the first 8 bytes will
1185 * be interpreted as a control setup packet, and the wLength field will be
1186 * used to automatically populate the \ref libusb_transfer::length "length"
1187 * field of the transfer. Therefore the recommended approach is:
1188 * -# Allocate a suitably sized data buffer (including space for control setup)
1189 * -# Call libusb_fill_control_setup()
1190 * -# If this is a host-to-device transfer with a data stage, put the data
1191 * in place after the setup packet
1192 * -# Call this function
1193 * -# Call libusb_submit_transfer()
1195 * It is also legal to pass a NULL buffer to this function, in which case this
1196 * function will not attempt to populate the length field. Remember that you
1197 * must then populate the buffer and length fields later.
1199 * \param transfer the transfer to populate
1200 * \param dev_handle handle of the device that will handle the transfer
1201 * \param buffer data buffer. If provided, this function will interpret the
1202 * first 8 bytes as a setup packet and infer the transfer length from that.
1203 * \param callback callback function to be invoked on transfer completion
1204 * \param user_data user data to pass to callback function
1205 * \param timeout timeout for the transfer in milliseconds
1207 static inline void libusb_fill_control_transfer(
1208 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1209 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1210 unsigned int timeout)
1212 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1213 transfer->dev_handle = dev_handle;
1214 transfer->endpoint = 0;
1215 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1216 transfer->timeout = timeout;
1217 transfer->buffer = buffer;
1219 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1220 + libusb_le16_to_cpu(setup->wLength);
1221 transfer->user_data = user_data;
1222 transfer->callback = callback;
1225 /** \ingroup asyncio
1226 * Helper function to populate the required \ref libusb_transfer fields
1227 * for a bulk transfer.
1229 * \param transfer the transfer to populate
1230 * \param dev_handle handle of the device that will handle the transfer
1231 * \param endpoint address of the endpoint where this transfer will be sent
1232 * \param buffer data buffer
1233 * \param length length of data buffer
1234 * \param callback callback function to be invoked on transfer completion
1235 * \param user_data user data to pass to callback function
1236 * \param timeout timeout for the transfer in milliseconds
1238 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1239 libusb_device_handle *dev_handle, unsigned char endpoint,
1240 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1241 void *user_data, unsigned int timeout)
1243 transfer->dev_handle = dev_handle;
1244 transfer->endpoint = endpoint;
1245 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1246 transfer->timeout = timeout;
1247 transfer->buffer = buffer;
1248 transfer->length = length;
1249 transfer->user_data = user_data;
1250 transfer->callback = callback;
1253 /** \ingroup asyncio
1254 * Helper function to populate the required \ref libusb_transfer fields
1255 * for an interrupt transfer.
1257 * \param transfer the transfer to populate
1258 * \param dev_handle handle of the device that will handle the transfer
1259 * \param endpoint address of the endpoint where this transfer will be sent
1260 * \param buffer data buffer
1261 * \param length length of data buffer
1262 * \param callback callback function to be invoked on transfer completion
1263 * \param user_data user data to pass to callback function
1264 * \param timeout timeout for the transfer in milliseconds
1266 static inline void libusb_fill_interrupt_transfer(
1267 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1268 unsigned char endpoint, unsigned char *buffer, int length,
1269 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1271 transfer->dev_handle = dev_handle;
1272 transfer->endpoint = endpoint;
1273 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1274 transfer->timeout = timeout;
1275 transfer->buffer = buffer;
1276 transfer->length = length;
1277 transfer->user_data = user_data;
1278 transfer->callback = callback;
1281 /** \ingroup asyncio
1282 * Helper function to populate the required \ref libusb_transfer fields
1283 * for an isochronous transfer.
1285 * \param transfer the transfer to populate
1286 * \param dev_handle handle of the device that will handle the transfer
1287 * \param endpoint address of the endpoint where this transfer will be sent
1288 * \param buffer data buffer
1289 * \param length length of data buffer
1290 * \param num_iso_packets the number of isochronous packets
1291 * \param callback callback function to be invoked on transfer completion
1292 * \param user_data user data to pass to callback function
1293 * \param timeout timeout for the transfer in milliseconds
1295 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1296 libusb_device_handle *dev_handle, unsigned char endpoint,
1297 unsigned char *buffer, int length, int num_iso_packets,
1298 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1300 transfer->dev_handle = dev_handle;
1301 transfer->endpoint = endpoint;
1302 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1303 transfer->timeout = timeout;
1304 transfer->buffer = buffer;
1305 transfer->length = length;
1306 transfer->num_iso_packets = num_iso_packets;
1307 transfer->user_data = user_data;
1308 transfer->callback = callback;
1311 /** \ingroup asyncio
1312 * Convenience function to set the length of all packets in an isochronous
1313 * transfer, based on the num_iso_packets field in the transfer structure.
1315 * \param transfer a transfer
1316 * \param length the length to set in each isochronous packet descriptor
1317 * \see libusb_get_max_packet_size()
1319 static inline void libusb_set_iso_packet_lengths(
1320 struct libusb_transfer *transfer, unsigned int length)
1323 for (i = 0; i < transfer->num_iso_packets; i++)
1324 transfer->iso_packet_desc[i].length = length;
1327 /** \ingroup asyncio
1328 * Convenience function to locate the position of an isochronous packet
1329 * within the buffer of an isochronous transfer.
1331 * This is a thorough function which loops through all preceding packets,
1332 * accumulating their lengths to find the position of the specified packet.
1333 * Typically you will assign equal lengths to each packet in the transfer,
1334 * and hence the above method is sub-optimal. You may wish to use
1335 * libusb_get_iso_packet_buffer_simple() instead.
1337 * \param transfer a transfer
1338 * \param packet the packet to return the address of
1339 * \returns the base address of the packet buffer inside the transfer buffer,
1340 * or NULL if the packet does not exist.
1341 * \see libusb_get_iso_packet_buffer_simple()
1343 static inline unsigned char *libusb_get_iso_packet_buffer(
1344 struct libusb_transfer *transfer, unsigned int packet)
1350 /* oops..slight bug in the API. packet is an unsigned int, but we use
1351 * signed integers almost everywhere else. range-check and convert to
1352 * signed to avoid compiler warnings. FIXME for libusb-2. */
1353 if (packet > INT_MAX)
1357 if (_packet >= transfer->num_iso_packets)
1360 for (i = 0; i < _packet; i++)
1361 offset += transfer->iso_packet_desc[i].length;
1363 return transfer->buffer + offset;
1366 /** \ingroup asyncio
1367 * Convenience function to locate the position of an isochronous packet
1368 * within the buffer of an isochronous transfer, for transfers where each
1369 * packet is of identical size.
1371 * This function relies on the assumption that every packet within the transfer
1372 * is of identical size to the first packet. Calculating the location of
1373 * the packet buffer is then just a simple calculation:
1374 * <tt>buffer + (packet_size * packet)</tt>
1376 * Do not use this function on transfers other than those that have identical
1377 * packet lengths for each packet.
1379 * \param transfer a transfer
1380 * \param packet the packet to return the address of
1381 * \returns the base address of the packet buffer inside the transfer buffer,
1382 * or NULL if the packet does not exist.
1383 * \see libusb_get_iso_packet_buffer()
1385 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1386 struct libusb_transfer *transfer, unsigned int packet)
1390 /* oops..slight bug in the API. packet is an unsigned int, but we use
1391 * signed integers almost everywhere else. range-check and convert to
1392 * signed to avoid compiler warnings. FIXME for libusb-2. */
1393 if (packet > INT_MAX)
1397 if (_packet >= transfer->num_iso_packets)
1400 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1405 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1406 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1407 unsigned char *data, uint16_t wLength, unsigned int timeout);
1409 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1410 unsigned char endpoint, unsigned char *data, int length,
1411 int *actual_length, unsigned int timeout);
1413 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1414 unsigned char endpoint, unsigned char *data, int length,
1415 int *actual_length, unsigned int timeout);
1418 * Retrieve a descriptor from the default control pipe.
1419 * This is a convenience function which formulates the appropriate control
1420 * message to retrieve the descriptor.
1422 * \param dev a device handle
1423 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1424 * \param desc_index the index of the descriptor to retrieve
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
1429 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1430 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1432 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1433 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1434 (uint16_t) length, 1000);
1438 * Retrieve a descriptor from a device.
1439 * This is a convenience function which formulates the appropriate control
1440 * message to retrieve the descriptor. The string returned is Unicode, as
1441 * detailed in the USB specifications.
1443 * \param dev a device handle
1444 * \param desc_index the index of the descriptor to retrieve
1445 * \param langid the language ID for the string descriptor
1446 * \param data output buffer for descriptor
1447 * \param length size of data buffer
1448 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1449 * \see libusb_get_string_descriptor_ascii()
1451 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1452 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1454 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1455 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1456 langid, data, (uint16_t) length, 1000);
1459 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1460 uint8_t desc_index, unsigned char *data, int length);
1462 /* polling and timeouts */
1464 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1465 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1466 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1467 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1468 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1469 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1470 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1471 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1473 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1474 struct timeval *tv);
1475 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1476 struct timeval *tv, int *completed);
1477 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1478 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1479 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1480 struct timeval *tv);
1481 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1482 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1483 struct timeval *tv);
1486 * File descriptor for polling
1488 struct libusb_pollfd {
1489 /** Numeric file descriptor */
1492 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1493 * should monitor this file descriptor for becoming ready to read from,
1494 * and POLLOUT indicates that you should monitor this file descriptor for
1495 * nonblocking write readiness. */
1500 * Callback function, invoked when a new file descriptor should be added
1501 * to the set of file descriptors monitored for events.
1502 * \param fd the new file descriptor
1503 * \param events events to monitor for, see \ref libusb_pollfd for a
1505 * \param user_data User data pointer specified in
1506 * libusb_set_pollfd_notifiers() call
1507 * \see libusb_set_pollfd_notifiers()
1509 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1513 * Callback function, invoked when a file descriptor should be removed from
1514 * the set of file descriptors being monitored for events. After returning
1515 * from this callback, do not use that file descriptor again.
1516 * \param fd the file descriptor to stop monitoring
1517 * \param user_data User data pointer specified in
1518 * libusb_set_pollfd_notifiers() call
1519 * \see libusb_set_pollfd_notifiers()
1521 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1523 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1524 libusb_context *ctx);
1525 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1526 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1529 /** \ingroup hotplug
1532 * Callbacks handles are generated by libusb_hotplug_register_callback()
1533 * and can be used to deregister callbacks. Callback handles are unique
1534 * per libusb_context and it is safe to call libusb_hotplug_deregister_callback()
1535 * on an already deregisted callback.
1537 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1539 * For more information, see \ref hotplug.
1541 typedef int libusb_hotplug_callback_handle;
1543 /** \ingroup hotplug
1545 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1547 * Flags for hotplug events */
1549 /** Arm the callback and fire it for all matching currently attached devices. */
1550 LIBUSB_HOTPLUG_ENUMERATE = 1,
1551 } libusb_hotplug_flag;
1553 /** \ingroup hotplug
1555 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1559 /** A device has been plugged in and is ready to use */
1560 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01,
1562 /** A device has left and is no longer available.
1563 * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
1564 * It is safe to call libusb_get_device_descriptor on a device that has left */
1565 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02,
1566 } libusb_hotplug_event;
1568 /** \ingroup hotplug
1569 * Wildcard matching for hotplug events */
1570 #define LIBUSB_HOTPLUG_MATCH_ANY -1
1572 /** \ingroup hotplug
1573 * Hotplug callback function type. When requesting hotplug event notifications,
1574 * you pass a pointer to a callback function of this type.
1576 * This callback may be called by an internal event thread and as such it is
1577 * recommended the callback do minimal processing before returning.
1579 * libusbx will call this function later, when a matching event had happened on
1580 * a matching device. See \ref hotplug for more information.
1582 * It is safe to call either libusb_hotplug_register_callback() or
1583 * libusb_hotplug_deregister_callback() from within a callback function.
1585 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1587 * \param libusb_context context of this notification
1588 * \param device libusb_device this event occurred on
1589 * \param event event that occurred
1590 * \param user_data user data provided when this callback was registered
1591 * \returns bool whether this callback is finished processing events.
1592 * returning 1 will cause this callback to be deregistered
1594 typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx,
1595 libusb_device *device,
1596 libusb_hotplug_event event,
1599 /** \ingroup hotplug
1600 * Register a hotplug callback function
1602 * Register a callback with the libusb_context. The callback will fire
1603 * when a matching event occurs on a matching device. The callback is
1604 * armed until either it is deregistered with libusb_hotplug_deregister_callback()
1605 * or the supplied callback returns 1 to indicate it is finished processing events.
1607 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1609 * \param[in] ctx context to register this callback with
1610 * \param[in] events bitwise or of events that will trigger this callback. See \ref
1611 * libusb_hotplug_event
1612 * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag
1613 * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1614 * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1615 * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1616 * \param[in] cb_fn the function to be invoked on a matching event/device
1617 * \param[in] user_data user data to pass to the callback function
1618 * \param[out] handle pointer to store the handle of the allocated callback (can be NULL)
1619 * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure
1621 int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx,
1622 libusb_hotplug_event events,
1623 libusb_hotplug_flag flags,
1624 int vendor_id, int product_id,
1626 libusb_hotplug_callback_fn cb_fn,
1628 libusb_hotplug_callback_handle *handle);
1630 /** \ingroup hotplug
1631 * Deregisters a hotplug callback.
1633 * Deregister a callback from a libusb_context. This function is safe to call from within
1634 * a hotplug callback.
1636 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1638 * \param[in] ctx context this callback is registered with
1639 * \param[in] handle the handle of the callback to deregister
1641 void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx,
1642 libusb_hotplug_callback_handle handle);