2 * Public libusb header file
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
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.
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.
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
25 /* on MS environments, the inline keyword is available in C++ only */
26 #define inline __inline
27 /* ssize_t is also not available (copy/paste from MinGW) */
28 #ifndef _SSIZE_T_DEFINED
29 #define _SSIZE_T_DEFINED
32 typedef __int64 ssize_t;
36 #endif /* _SSIZE_T_DEFINED */
39 /* stdint.h is also not usually available on MS */
40 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
41 typedef unsigned __int8 uint8_t;
42 typedef unsigned __int16 uint16_t;
47 #include <sys/types.h>
51 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
55 /* 'interface' might be defined as a macro on Windows, so we need to
56 * undefine it so as not to break the current libusb API, because
57 * libusb_config_descriptor has an 'interface' member
58 * As this can be problematic if you include windows.h after libusb.h
59 * in your sources, we force windows.h to be included first. */
60 #if defined(_WIN32) || defined(__CYGWIN__)
62 #if defined(interface)
69 * libusb's Windows calling convention.
71 * Under Windows, the selection of available compilers and configurations
72 * means that, unlike other platforms, there is not <em>one true calling
73 * convention</em> (calling convention: the manner in which parameters are
74 * passed to funcions in the generated assembly code).
76 * Matching the Windows API itself, libusb uses the WINAPI convention (which
77 * translates to the <tt>stdcall</tt> convention) and guarantees that the
78 * library is compiled in this way. The public header file also includes
79 * appropriate annotations so that your own software will use the right
80 * convention, even if another convention is being used by default within
83 * The one consideration that you must apply in your software is to mark
84 * all functions which you use as libusb callbacks with this LIBUSB_CALL
85 * annotation, so that they too get compiled for the correct calling
88 * On non-Windows operating systems, this macro is defined as nothing. This
89 * means that you can apply it to your code without worrying about
90 * cross-platform compatibility.
92 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
93 * functions. You'd think that declaration would be enough, but cygwin will
94 * complain about conflicting types unless both are marked this way.
95 * The placement of this macro is important too; it must appear after the
96 * return type, before the function name. See internal documentation for
99 #if defined(_WIN32) || defined(__CYGWIN__)
100 #define LIBUSB_CALL WINAPI
109 /** \def libusb_cpu_to_le16
111 * Convert a 16-bit value from host-endian to little-endian format. On
112 * little endian systems, this function does nothing. On big endian systems,
113 * the bytes are swapped.
114 * \param x the host-endian value to convert
115 * \returns the value in little-endian byte order
117 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
124 _tmp.b8[0] = x & 0xff;
128 /** \def libusb_le16_to_cpu
130 * Convert a 16-bit value from little-endian to host-endian format. On
131 * little endian systems, this function does nothing. On big endian systems,
132 * the bytes are swapped.
133 * \param x the little-endian value to convert
134 * \returns the value in host-endian byte order
136 #define libusb_le16_to_cpu libusb_cpu_to_le16
138 /* standard USB stuff */
141 * Device and/or Interface Class codes */
142 enum libusb_class_code {
143 /** In the context of a \ref libusb_device_descriptor "device descriptor",
144 * this bDeviceClass value indicates that each interface specifies its
145 * own class information and all interfaces operate independently.
147 LIBUSB_CLASS_PER_INTERFACE = 0,
150 LIBUSB_CLASS_AUDIO = 1,
152 /** Communications class */
153 LIBUSB_CLASS_COMM = 2,
155 /** Human Interface Device class */
156 LIBUSB_CLASS_HID = 3,
159 LIBUSB_CLASS_PHYSICAL = 5,
162 LIBUSB_CLASS_PRINTER = 7,
165 LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
166 LIBUSB_CLASS_IMAGE = 6,
168 /** Mass storage class */
169 LIBUSB_CLASS_MASS_STORAGE = 8,
172 LIBUSB_CLASS_HUB = 9,
175 LIBUSB_CLASS_DATA = 10,
178 LIBUSB_CLASS_SMART_CARD = 0x0b,
180 /** Content Security */
181 LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
184 LIBUSB_CLASS_VIDEO = 0x0e,
186 /** Personal Healthcare */
187 LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
189 /** Diagnostic Device */
190 LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
192 /** Wireless class */
193 LIBUSB_CLASS_WIRELESS = 0xe0,
195 /** Application class */
196 LIBUSB_CLASS_APPLICATION = 0xfe,
198 /** Class is vendor-specific */
199 LIBUSB_CLASS_VENDOR_SPEC = 0xff
203 * Descriptor types as defined by the USB specification. */
204 enum libusb_descriptor_type {
205 /** Device descriptor. See libusb_device_descriptor. */
206 LIBUSB_DT_DEVICE = 0x01,
208 /** Configuration descriptor. See libusb_config_descriptor. */
209 LIBUSB_DT_CONFIG = 0x02,
211 /** String descriptor */
212 LIBUSB_DT_STRING = 0x03,
214 /** Interface descriptor. See libusb_interface_descriptor. */
215 LIBUSB_DT_INTERFACE = 0x04,
217 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
218 LIBUSB_DT_ENDPOINT = 0x05,
220 /** HID descriptor */
221 LIBUSB_DT_HID = 0x21,
223 /** HID report descriptor */
224 LIBUSB_DT_REPORT = 0x22,
226 /** Physical descriptor */
227 LIBUSB_DT_PHYSICAL = 0x23,
229 /** Hub descriptor */
233 /* Descriptor sizes per descriptor type */
234 #define LIBUSB_DT_DEVICE_SIZE 18
235 #define LIBUSB_DT_CONFIG_SIZE 9
236 #define LIBUSB_DT_INTERFACE_SIZE 9
237 #define LIBUSB_DT_ENDPOINT_SIZE 7
238 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
239 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
241 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
242 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
245 * Endpoint direction. Values for bit 7 of the
246 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
248 enum libusb_endpoint_direction {
249 /** In: device-to-host */
250 LIBUSB_ENDPOINT_IN = 0x80,
252 /** Out: host-to-device */
253 LIBUSB_ENDPOINT_OUT = 0x00
256 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
259 * Endpoint transfer type. Values for bits 0:1 of the
260 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
262 enum libusb_transfer_type {
263 /** Control endpoint */
264 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
266 /** Isochronous endpoint */
267 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
270 LIBUSB_TRANSFER_TYPE_BULK = 2,
272 /** Interrupt endpoint */
273 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
277 * Standard requests, as defined in table 9-3 of the USB2 specifications */
278 enum libusb_standard_request {
279 /** Request status of the specific recipient */
280 LIBUSB_REQUEST_GET_STATUS = 0x00,
282 /** Clear or disable a specific feature */
283 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
285 /* 0x02 is reserved */
287 /** Set or enable a specific feature */
288 LIBUSB_REQUEST_SET_FEATURE = 0x03,
290 /* 0x04 is reserved */
292 /** Set device address for all future accesses */
293 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
295 /** Get the specified descriptor */
296 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
298 /** Used to update existing descriptors or add new descriptors */
299 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
301 /** Get the current device configuration value */
302 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
304 /** Set device configuration */
305 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
307 /** Return the selected alternate setting for the specified interface */
308 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
310 /** Select an alternate interface for the specified interface */
311 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
313 /** Set then report an endpoint's synchronization frame */
314 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
318 * Request type bits of the
319 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
321 enum libusb_request_type {
323 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
326 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
329 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
332 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
336 * Recipient bits of the
337 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
338 * transfers. Values 4 through 31 are reserved. */
339 enum libusb_request_recipient {
341 LIBUSB_RECIPIENT_DEVICE = 0x00,
344 LIBUSB_RECIPIENT_INTERFACE = 0x01,
347 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
350 LIBUSB_RECIPIENT_OTHER = 0x03
353 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
356 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
357 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
358 * libusb_endpoint_descriptor.
360 enum libusb_iso_sync_type {
361 /** No synchronization */
362 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
365 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
368 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
371 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
374 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
377 * Usage type for isochronous endpoints. Values for bits 4:5 of the
378 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
379 * libusb_endpoint_descriptor.
381 enum libusb_iso_usage_type {
383 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
385 /** Feedback endpoint */
386 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
388 /** Implicit feedback Data endpoint */
389 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
393 * A structure representing the standard USB device descriptor. This
394 * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
395 * All multiple-byte fields are represented in host-endian format.
397 struct libusb_device_descriptor {
398 /** Size of this descriptor (in bytes) */
401 /** Descriptor type. Will have value
402 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
404 uint8_t bDescriptorType;
406 /** USB specification release number in binary-coded decimal. A value of
407 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
410 /** USB-IF class code for the device. See \ref libusb_class_code. */
411 uint8_t bDeviceClass;
413 /** USB-IF subclass code for the device, qualified by the bDeviceClass
415 uint8_t bDeviceSubClass;
417 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
418 * bDeviceSubClass values */
419 uint8_t bDeviceProtocol;
421 /** Maximum packet size for endpoint 0 */
422 uint8_t bMaxPacketSize0;
424 /** USB-IF vendor ID */
427 /** USB-IF product ID */
430 /** Device release number in binary-coded decimal */
433 /** Index of string descriptor describing manufacturer */
434 uint8_t iManufacturer;
436 /** Index of string descriptor describing product */
439 /** Index of string descriptor containing device serial number */
440 uint8_t iSerialNumber;
442 /** Number of possible configurations */
443 uint8_t bNumConfigurations;
447 * A structure representing the standard USB endpoint descriptor. This
448 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
449 * All multiple-byte fields are represented in host-endian format.
451 struct libusb_endpoint_descriptor {
452 /** Size of this descriptor (in bytes) */
455 /** Descriptor type. Will have value
456 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
458 uint8_t bDescriptorType;
460 /** The address of the endpoint described by this descriptor. Bits 0:3 are
461 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
462 * see \ref libusb_endpoint_direction.
464 uint8_t bEndpointAddress;
466 /** Attributes which apply to the endpoint when it is configured using
467 * the bConfigurationValue. Bits 0:1 determine the transfer type and
468 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
469 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
470 * Bits 4:5 are also only used for isochronous endpoints and correspond to
471 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
473 uint8_t bmAttributes;
475 /** Maximum packet size this endpoint is capable of sending/receiving. */
476 uint16_t wMaxPacketSize;
478 /** Interval for polling endpoint for data transfers. */
481 /** For audio devices only: the rate at which synchronization feedback
485 /** For audio devices only: the address if the synch endpoint */
486 uint8_t bSynchAddress;
488 /** Extra descriptors. If libusb encounters unknown endpoint descriptors,
489 * it will store them here, should you wish to parse them. */
490 const unsigned char *extra;
492 /** Length of the extra descriptors, in bytes. */
497 * A structure representing the standard USB interface descriptor. This
498 * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
499 * All multiple-byte fields are represented in host-endian format.
501 struct libusb_interface_descriptor {
502 /** Size of this descriptor (in bytes) */
505 /** Descriptor type. Will have value
506 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
507 * in this context. */
508 uint8_t bDescriptorType;
510 /** Number of this interface */
511 uint8_t bInterfaceNumber;
513 /** Value used to select this alternate setting for this interface */
514 uint8_t bAlternateSetting;
516 /** Number of endpoints used by this interface (excluding the control
518 uint8_t bNumEndpoints;
520 /** USB-IF class code for this interface. See \ref libusb_class_code. */
521 uint8_t bInterfaceClass;
523 /** USB-IF subclass code for this interface, qualified by the
524 * bInterfaceClass value */
525 uint8_t bInterfaceSubClass;
527 /** USB-IF protocol code for this interface, qualified by the
528 * bInterfaceClass and bInterfaceSubClass values */
529 uint8_t bInterfaceProtocol;
531 /** Index of string descriptor describing this interface */
534 /** Array of endpoint descriptors. This length of this array is determined
535 * by the bNumEndpoints field. */
536 const struct libusb_endpoint_descriptor *endpoint;
538 /** Extra descriptors. If libusb encounters unknown interface descriptors,
539 * it will store them here, should you wish to parse them. */
540 const unsigned char *extra;
542 /** Length of the extra descriptors, in bytes. */
547 * A collection of alternate settings for a particular USB interface.
549 struct libusb_interface {
550 /** Array of interface descriptors. The length of this array is determined
551 * by the num_altsetting field. */
552 const struct libusb_interface_descriptor *altsetting;
554 /** The number of alternate settings that belong to this interface */
559 * A structure representing the standard USB configuration descriptor. This
560 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
561 * All multiple-byte fields are represented in host-endian format.
563 struct libusb_config_descriptor {
564 /** Size of this descriptor (in bytes) */
567 /** Descriptor type. Will have value
568 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
569 * in this context. */
570 uint8_t bDescriptorType;
572 /** Total length of data returned for this configuration */
573 uint16_t wTotalLength;
575 /** Number of interfaces supported by this configuration */
576 uint8_t bNumInterfaces;
578 /** Identifier value for this configuration */
579 uint8_t bConfigurationValue;
581 /** Index of string descriptor describing this configuration */
582 uint8_t iConfiguration;
584 /** Configuration characteristics */
585 uint8_t bmAttributes;
587 /** Maximum power consumption of the USB device from this bus in this
588 * configuration when the device is fully opreation. Expressed in units
592 /** Array of interfaces supported by this configuration. The length of
593 * this array is determined by the bNumInterfaces field. */
594 const struct libusb_interface *interface;
596 /** Extra descriptors. If libusb encounters unknown configuration
597 * descriptors, it will store them here, should you wish to parse them. */
598 const unsigned char *extra;
600 /** Length of the extra descriptors, in bytes. */
605 * Setup packet for control transfers. */
606 struct libusb_control_setup {
607 /** Request type. Bits 0:4 determine recipient, see
608 * \ref libusb_request_recipient. Bits 5:6 determine type, see
609 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
610 * \ref libusb_endpoint_direction.
612 uint8_t bmRequestType;
614 /** Request. If the type bits of bmRequestType are equal to
615 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
616 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
617 * \ref libusb_standard_request. For other cases, use of this field is
618 * application-specific. */
621 /** Value. Varies according to request */
624 /** Index. Varies according to request, typically used to pass an index
628 /** Number of bytes to transfer */
632 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
636 struct libusb_context;
637 struct libusb_device;
638 struct libusb_device_handle;
641 * Structure representing a libusb session. The concept of individual libusb
642 * sessions allows for your program to use two libraries (or dynamically
643 * load two modules) which both independently use libusb. This will prevent
644 * interference between the individual libusb users - for example
645 * libusb_set_debug() will not affect the other user of the library, and
646 * libusb_exit() will not destroy resources that the other user is still
649 * Sessions are created by libusb_init() and destroyed through libusb_exit().
650 * If your application is guaranteed to only ever include a single libusb
651 * user (i.e. you), you do not have to worry about contexts: pass NULL in
652 * every function call where a context is required. The default context
655 * For more information, see \ref contexts.
657 typedef struct libusb_context libusb_context;
660 * Structure representing a USB device detected on the system. This is an
661 * opaque type for which you are only ever provided with a pointer, usually
662 * originating from libusb_get_device_list().
664 * Certain operations can be performed on a device, but in order to do any
665 * I/O you will have to first obtain a device handle using libusb_open().
667 * Devices are reference counted with libusb_device_ref() and
668 * libusb_device_unref(), and are freed when the reference count reaches 0.
669 * New devices presented by libusb_get_device_list() have a reference count of
670 * 1, and libusb_free_device_list() can optionally decrease the reference count
671 * on all devices in the list. libusb_open() adds another reference which is
672 * later destroyed by libusb_close().
674 typedef struct libusb_device libusb_device;
678 * Structure representing a handle on a USB device. This is an opaque type for
679 * which you are only ever provided with a pointer, usually originating from
682 * A device handle is used to perform I/O and other operations. When finished
683 * with a device handle, you should call libusb_close().
685 typedef struct libusb_device_handle libusb_device_handle;
688 * Speed codes. Indicates the speed at which the device is operating.
691 /** The OS doesn't report or know the device speed. */
692 LIBUSB_SPEED_UNKNOWN = 0,
694 /** The device is operating at low speed (1.5MBit/s). */
695 LIBUSB_SPEED_LOW = 1,
697 /** The device is operating at full speed (12MBit/s). */
698 LIBUSB_SPEED_FULL = 2,
700 /** The device is operating at high speed (480MBit/s). */
701 LIBUSB_SPEED_HIGH = 3,
703 /** The device is operating at super speed (5000MBit/s). */
704 LIBUSB_SPEED_SUPER = 4,
708 * Error codes. Most libusb functions return 0 on success or one of these
712 /** Success (no error) */
715 /** Input/output error */
716 LIBUSB_ERROR_IO = -1,
718 /** Invalid parameter */
719 LIBUSB_ERROR_INVALID_PARAM = -2,
721 /** Access denied (insufficient permissions) */
722 LIBUSB_ERROR_ACCESS = -3,
724 /** No such device (it may have been disconnected) */
725 LIBUSB_ERROR_NO_DEVICE = -4,
727 /** Entity not found */
728 LIBUSB_ERROR_NOT_FOUND = -5,
731 LIBUSB_ERROR_BUSY = -6,
733 /** Operation timed out */
734 LIBUSB_ERROR_TIMEOUT = -7,
737 LIBUSB_ERROR_OVERFLOW = -8,
740 LIBUSB_ERROR_PIPE = -9,
742 /** System call interrupted (perhaps due to signal) */
743 LIBUSB_ERROR_INTERRUPTED = -10,
745 /** Insufficient memory */
746 LIBUSB_ERROR_NO_MEM = -11,
748 /** Operation not supported or unimplemented on this platform */
749 LIBUSB_ERROR_NOT_SUPPORTED = -12,
752 LIBUSB_ERROR_OTHER = -99
756 * Transfer status codes */
757 enum libusb_transfer_status {
758 /** Transfer completed without error. Note that this does not indicate
759 * that the entire amount of requested data was transferred. */
760 LIBUSB_TRANSFER_COMPLETED,
762 /** Transfer failed */
763 LIBUSB_TRANSFER_ERROR,
765 /** Transfer timed out */
766 LIBUSB_TRANSFER_TIMED_OUT,
768 /** Transfer was cancelled */
769 LIBUSB_TRANSFER_CANCELLED,
771 /** For bulk/interrupt endpoints: halt condition detected (endpoint
772 * stalled). For control endpoints: control request not supported. */
773 LIBUSB_TRANSFER_STALL,
775 /** Device was disconnected */
776 LIBUSB_TRANSFER_NO_DEVICE,
778 /** Device sent more data than requested */
779 LIBUSB_TRANSFER_OVERFLOW
783 * libusb_transfer.flags values */
784 enum libusb_transfer_flags {
785 /** Report short frames as errors */
786 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
788 /** Automatically free() transfer buffer during libusb_free_transfer() */
789 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
791 /** Automatically call libusb_free_transfer() after callback returns.
792 * If this flag is set, it is illegal to call libusb_free_transfer()
793 * from your transfer callback, as this will result in a double-free
794 * when this flag is acted upon. */
795 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
799 * Isochronous packet descriptor. */
800 struct libusb_iso_packet_descriptor {
801 /** Length of data to request in this packet */
804 /** Amount of data that was actually transferred */
805 unsigned int actual_length;
807 /** Status code for this packet */
808 enum libusb_transfer_status status;
811 struct libusb_transfer;
814 * Asynchronous transfer callback function type. When submitting asynchronous
815 * transfers, you pass a pointer to a callback function of this type via the
816 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
817 * structure. libusb will call this function later, when the transfer has
818 * completed or failed. See \ref asyncio for more information.
819 * \param transfer The libusb_transfer struct the callback function is being
822 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
825 * The generic USB transfer structure. The user populates this structure and
826 * then submits it in order to request a transfer. After the transfer has
827 * completed, the library populates the transfer with the results and passes
828 * it back to the user.
830 struct libusb_transfer {
831 /** Handle of the device that this transfer will be submitted to */
832 libusb_device_handle *dev_handle;
834 /** A bitwise OR combination of \ref libusb_transfer_flags. */
837 /** Address of the endpoint where this transfer will be sent. */
838 unsigned char endpoint;
840 /** Type of the endpoint from \ref libusb_transfer_type */
843 /** Timeout for this transfer in millseconds. A value of 0 indicates no
845 unsigned int timeout;
847 /** The status of the transfer. Read-only, and only for use within
848 * transfer callback function.
850 * If this is an isochronous transfer, this field may read COMPLETED even
851 * if there were errors in the frames. Use the
852 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
853 * to determine if errors occurred. */
854 enum libusb_transfer_status status;
856 /** Length of the data buffer */
859 /** Actual length of data that was transferred. Read-only, and only for
860 * use within transfer callback function. Not valid for isochronous
861 * endpoint transfers. */
864 /** Callback function. This will be invoked when the transfer completes,
865 * fails, or is cancelled. */
866 libusb_transfer_cb_fn callback;
868 /** User context data to pass to the callback function. */
872 unsigned char *buffer;
874 /** Number of isochronous packets. Only used for I/O with isochronous
878 /** Isochronous packet descriptors, for isochronous transfers only. */
879 struct libusb_iso_packet_descriptor iso_packet_desc
880 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
881 [] /* valid C99 code */
883 [0] /* non-standard, but usually working code */
888 int LIBUSB_CALL libusb_init(libusb_context **ctx);
889 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
890 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
892 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
893 libusb_device ***list);
894 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
896 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
897 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
899 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
901 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
902 struct libusb_device_descriptor *desc);
903 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
904 struct libusb_config_descriptor **config);
905 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
906 uint8_t config_index, struct libusb_config_descriptor **config);
907 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
908 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
909 void LIBUSB_CALL libusb_free_config_descriptor(
910 struct libusb_config_descriptor *config);
911 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
912 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
913 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
914 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
915 unsigned char endpoint);
916 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
917 unsigned char endpoint);
919 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
920 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
921 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
923 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
925 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
926 int interface_number);
927 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
928 int interface_number);
930 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
931 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
933 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
934 int interface_number, int alternate_setting);
935 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
936 unsigned char endpoint);
937 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
939 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
940 int interface_number);
941 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
942 int interface_number);
943 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
944 int interface_number);
949 * Get the data section of a control transfer. This convenience function is here
950 * to remind you that the data does not start until 8 bytes into the actual
951 * buffer, as the setup packet comes first.
953 * Calling this function only makes sense from a transfer callback function,
954 * or situations where you have already allocated a suitably sized buffer at
957 * \param transfer a transfer
958 * \returns pointer to the first byte of the data section
960 static inline unsigned char *libusb_control_transfer_get_data(
961 struct libusb_transfer *transfer)
963 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
967 * Get the control setup packet of a control transfer. This convenience
968 * function is here to remind you that the control setup occupies the first
969 * 8 bytes of the transfer data buffer.
971 * Calling this function only makes sense from a transfer callback function,
972 * or situations where you have already allocated a suitably sized buffer at
975 * \param transfer a transfer
976 * \returns a casted pointer to the start of the transfer data buffer
978 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
979 struct libusb_transfer *transfer)
981 return (struct libusb_control_setup *) transfer->buffer;
985 * Helper function to populate the setup packet (first 8 bytes of the data
986 * buffer) for a control transfer. The wIndex, wValue and wLength values should
987 * be given in host-endian byte order.
989 * \param buffer buffer to output the setup packet into
990 * \param bmRequestType see the
991 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
992 * \ref libusb_control_setup
993 * \param bRequest see the
994 * \ref libusb_control_setup::bRequest "bRequest" field of
995 * \ref libusb_control_setup
996 * \param wValue see the
997 * \ref libusb_control_setup::wValue "wValue" field of
998 * \ref libusb_control_setup
999 * \param wIndex see the
1000 * \ref libusb_control_setup::wIndex "wIndex" field of
1001 * \ref libusb_control_setup
1002 * \param wLength see the
1003 * \ref libusb_control_setup::wLength "wLength" field of
1004 * \ref libusb_control_setup
1006 static inline void libusb_fill_control_setup(unsigned char *buffer,
1007 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1010 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1011 setup->bmRequestType = bmRequestType;
1012 setup->bRequest = bRequest;
1013 setup->wValue = libusb_cpu_to_le16(wValue);
1014 setup->wIndex = libusb_cpu_to_le16(wIndex);
1015 setup->wLength = libusb_cpu_to_le16(wLength);
1018 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1019 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1020 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1021 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1023 /** \ingroup asyncio
1024 * Helper function to populate the required \ref libusb_transfer fields
1025 * for a control transfer.
1027 * If you pass a transfer buffer to this function, the first 8 bytes will
1028 * be interpreted as a control setup packet, and the wLength field will be
1029 * used to automatically populate the \ref libusb_transfer::length "length"
1030 * field of the transfer. Therefore the recommended approach is:
1031 * -# Allocate a suitably sized data buffer (including space for control setup)
1032 * -# Call libusb_fill_control_setup()
1033 * -# If this is a host-to-device transfer with a data stage, put the data
1034 * in place after the setup packet
1035 * -# Call this function
1036 * -# Call libusb_submit_transfer()
1038 * It is also legal to pass a NULL buffer to this function, in which case this
1039 * function will not attempt to populate the length field. Remember that you
1040 * must then populate the buffer and length fields later.
1042 * \param transfer the transfer to populate
1043 * \param dev_handle handle of the device that will handle the transfer
1044 * \param buffer data buffer. If provided, this function will interpret the
1045 * first 8 bytes as a setup packet and infer the transfer length from that.
1046 * \param callback callback function to be invoked on transfer completion
1047 * \param user_data user data to pass to callback function
1048 * \param timeout timeout for the transfer in milliseconds
1050 static inline void libusb_fill_control_transfer(
1051 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1052 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1053 unsigned int timeout)
1055 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1056 transfer->dev_handle = dev_handle;
1057 transfer->endpoint = 0;
1058 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1059 transfer->timeout = timeout;
1060 transfer->buffer = buffer;
1062 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1063 + libusb_le16_to_cpu(setup->wLength);
1064 transfer->user_data = user_data;
1065 transfer->callback = callback;
1068 /** \ingroup asyncio
1069 * Helper function to populate the required \ref libusb_transfer fields
1070 * for a bulk transfer.
1072 * \param transfer the transfer to populate
1073 * \param dev_handle handle of the device that will handle the transfer
1074 * \param endpoint address of the endpoint where this transfer will be sent
1075 * \param buffer data buffer
1076 * \param length length of data buffer
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
1081 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1082 libusb_device_handle *dev_handle, unsigned char endpoint,
1083 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1084 void *user_data, unsigned int timeout)
1086 transfer->dev_handle = dev_handle;
1087 transfer->endpoint = endpoint;
1088 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1089 transfer->timeout = timeout;
1090 transfer->buffer = buffer;
1091 transfer->length = length;
1092 transfer->user_data = user_data;
1093 transfer->callback = callback;
1096 /** \ingroup asyncio
1097 * Helper function to populate the required \ref libusb_transfer fields
1098 * for an interrupt transfer.
1100 * \param transfer the transfer to populate
1101 * \param dev_handle handle of the device that will handle the transfer
1102 * \param endpoint address of the endpoint where this transfer will be sent
1103 * \param buffer data buffer
1104 * \param length length of data buffer
1105 * \param callback callback function to be invoked on transfer completion
1106 * \param user_data user data to pass to callback function
1107 * \param timeout timeout for the transfer in milliseconds
1109 static inline void libusb_fill_interrupt_transfer(
1110 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1111 unsigned char endpoint, unsigned char *buffer, int length,
1112 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1114 transfer->dev_handle = dev_handle;
1115 transfer->endpoint = endpoint;
1116 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1117 transfer->timeout = timeout;
1118 transfer->buffer = buffer;
1119 transfer->length = length;
1120 transfer->user_data = user_data;
1121 transfer->callback = callback;
1124 /** \ingroup asyncio
1125 * Helper function to populate the required \ref libusb_transfer fields
1126 * for an isochronous transfer.
1128 * \param transfer the transfer to populate
1129 * \param dev_handle handle of the device that will handle the transfer
1130 * \param endpoint address of the endpoint where this transfer will be sent
1131 * \param buffer data buffer
1132 * \param length length of data buffer
1133 * \param num_iso_packets the number of isochronous packets
1134 * \param callback callback function to be invoked on transfer completion
1135 * \param user_data user data to pass to callback function
1136 * \param timeout timeout for the transfer in milliseconds
1138 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1139 libusb_device_handle *dev_handle, unsigned char endpoint,
1140 unsigned char *buffer, int length, int num_iso_packets,
1141 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1143 transfer->dev_handle = dev_handle;
1144 transfer->endpoint = endpoint;
1145 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1146 transfer->timeout = timeout;
1147 transfer->buffer = buffer;
1148 transfer->length = length;
1149 transfer->num_iso_packets = num_iso_packets;
1150 transfer->user_data = user_data;
1151 transfer->callback = callback;
1154 /** \ingroup asyncio
1155 * Convenience function to set the length of all packets in an isochronous
1156 * transfer, based on the num_iso_packets field in the transfer structure.
1158 * \param transfer a transfer
1159 * \param length the length to set in each isochronous packet descriptor
1160 * \see libusb_get_max_packet_size()
1162 static inline void libusb_set_iso_packet_lengths(
1163 struct libusb_transfer *transfer, unsigned int length)
1166 for (i = 0; i < transfer->num_iso_packets; i++)
1167 transfer->iso_packet_desc[i].length = length;
1170 /** \ingroup asyncio
1171 * Convenience function to locate the position of an isochronous packet
1172 * within the buffer of an isochronous transfer.
1174 * This is a thorough function which loops through all preceding packets,
1175 * accumulating their lengths to find the position of the specified packet.
1176 * Typically you will assign equal lengths to each packet in the transfer,
1177 * and hence the above method is sub-optimal. You may wish to use
1178 * libusb_get_iso_packet_buffer_simple() instead.
1180 * \param transfer a transfer
1181 * \param packet the packet to return the address of
1182 * \returns the base address of the packet buffer inside the transfer buffer,
1183 * or NULL if the packet does not exist.
1184 * \see libusb_get_iso_packet_buffer_simple()
1186 static inline unsigned char *libusb_get_iso_packet_buffer(
1187 struct libusb_transfer *transfer, unsigned int packet)
1193 /* oops..slight bug in the API. packet is an unsigned int, but we use
1194 * signed integers almost everywhere else. range-check and convert to
1195 * signed to avoid compiler warnings. FIXME for libusb-2. */
1196 if (packet > INT_MAX)
1200 if (_packet >= transfer->num_iso_packets)
1203 for (i = 0; i < _packet; i++)
1204 offset += transfer->iso_packet_desc[i].length;
1206 return transfer->buffer + offset;
1209 /** \ingroup asyncio
1210 * Convenience function to locate the position of an isochronous packet
1211 * within the buffer of an isochronous transfer, for transfers where each
1212 * packet is of identical size.
1214 * This function relies on the assumption that every packet within the transfer
1215 * is of identical size to the first packet. Calculating the location of
1216 * the packet buffer is then just a simple calculation:
1217 * <tt>buffer + (packet_size * packet)</tt>
1219 * Do not use this function on transfers other than those that have identical
1220 * packet lengths for each packet.
1222 * \param transfer a transfer
1223 * \param packet the packet to return the address of
1224 * \returns the base address of the packet buffer inside the transfer buffer,
1225 * or NULL if the packet does not exist.
1226 * \see libusb_get_iso_packet_buffer()
1228 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1229 struct libusb_transfer *transfer, unsigned int packet)
1233 /* oops..slight bug in the API. packet is an unsigned int, but we use
1234 * signed integers almost everywhere else. range-check and convert to
1235 * signed to avoid compiler warnings. FIXME for libusb-2. */
1236 if (packet > INT_MAX)
1240 if (_packet >= transfer->num_iso_packets)
1243 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1248 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1249 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1250 unsigned char *data, uint16_t wLength, unsigned int timeout);
1252 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1253 unsigned char endpoint, unsigned char *data, int length,
1254 int *actual_length, unsigned int timeout);
1256 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1257 unsigned char endpoint, unsigned char *data, int length,
1258 int *actual_length, unsigned int timeout);
1261 * Retrieve a descriptor from the default control pipe.
1262 * This is a convenience function which formulates the appropriate control
1263 * message to retrieve the descriptor.
1265 * \param dev a device handle
1266 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1267 * \param desc_index the index of the descriptor to retrieve
1268 * \param data output buffer for descriptor
1269 * \param length size of data buffer
1270 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1272 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1273 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1275 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1276 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1277 (uint16_t) length, 1000);
1281 * Retrieve a descriptor from a device.
1282 * This is a convenience function which formulates the appropriate control
1283 * message to retrieve the descriptor. The string returned is Unicode, as
1284 * detailed in the USB specifications.
1286 * \param dev a device handle
1287 * \param desc_index the index of the descriptor to retrieve
1288 * \param langid the language ID for the string descriptor
1289 * \param data output buffer for descriptor
1290 * \param length size of data buffer
1291 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1292 * \see libusb_get_string_descriptor_ascii()
1294 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1295 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1297 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1298 LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
1299 langid, data, (uint16_t) length, 1000);
1302 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1303 uint8_t desc_index, unsigned char *data, int length);
1305 /* polling and timeouts */
1307 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1308 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1309 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1310 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1311 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1312 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1313 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1314 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1316 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1317 struct timeval *tv);
1318 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1319 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1320 struct timeval *tv);
1321 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1322 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1323 struct timeval *tv);
1326 * File descriptor for polling
1328 struct libusb_pollfd {
1329 /** Numeric file descriptor */
1332 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1333 * should monitor this file descriptor for becoming ready to read from,
1334 * and POLLOUT indicates that you should monitor this file descriptor for
1335 * nonblocking write readiness. */
1340 * Callback function, invoked when a new file descriptor should be added
1341 * to the set of file descriptors monitored for events.
1342 * \param fd the new file descriptor
1343 * \param events events to monitor for, see \ref libusb_pollfd for a
1345 * \param user_data User data pointer specified in
1346 * libusb_set_pollfd_notifiers() call
1347 * \see libusb_set_pollfd_notifiers()
1349 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1353 * Callback function, invoked when a file descriptor should be removed from
1354 * the set of file descriptors being monitored for events. After returning
1355 * from this callback, do not use that file descriptor again.
1356 * \param fd the file descriptor to stop monitoring
1357 * \param user_data User data pointer specified in
1358 * libusb_set_pollfd_notifiers() call
1359 * \see libusb_set_pollfd_notifiers()
1361 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1363 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1364 libusb_context *ctx);
1365 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1366 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,