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
24 /* MSVC doesn't like inline, but does accept __inline ?? */
26 #define inline __inline
30 #include <sys/types.h>
34 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
38 /* 'interface' might be defined as a macro on Windows, so we need to
39 * undefine it so as not to break the current libusb API, because
40 * libusb_config_descriptor has an 'interface' member
41 * As this can be problematic if you include windows.h after libusb.h
42 * in your sources, we force windows.h to be included first. */
43 #if defined(_WIN32) || defined(__CYGWIN__)
45 #if defined(interface)
52 * libusb's Windows calling convention.
54 * Under Windows, the selection of available compilers and configurations
55 * means that, unlike other platforms, there is not <em>one true calling
56 * convention</em> (calling convention: the manner in which parameters are
57 * passed to funcions in the generated assembly code).
59 * Matching the Windows API itself, libusb uses the WINAPI convention (which
60 * translates to the <tt>stdcall</tt> convention) and guarantees that the
61 * library is compiled in this way. The public header file also includes
62 * appropriate annotations so that your own software will use the right
63 * convention, even if another convention is being used by default within
66 * The one consideration that you must apply in your software is to mark
67 * all functions which you use as libusb callbacks with this LIBUSB_CALL
68 * annotation, so that they too get compiled for the correct calling
71 * On non-Windows operating systems, this macro is defined as nothing. This
72 * means that you can apply it to your code without worrying about
73 * cross-platform compatibility.
75 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
76 * functions. You'd think that declaration would be enough, but cygwin will
77 * complain about conflicting types unless both are marked this way.
78 * The placement of this macro is important too; it must appear after the
79 * return type, before the function name. See internal documentation for
82 #if defined(_WIN32) || defined(__CYGWIN__)
83 #define LIBUSB_CALL WINAPI
92 /** \def libusb_cpu_to_le16
94 * Convert a 16-bit value from host-endian to little-endian format. On
95 * little endian systems, this function does nothing. On big endian systems,
96 * the bytes are swapped.
97 * \param x the host-endian value to convert
98 * \returns the value in little-endian byte order
100 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
107 _tmp.b8[0] = x & 0xff;
111 /** \def libusb_le16_to_cpu
113 * Convert a 16-bit value from little-endian to host-endian format. On
114 * little endian systems, this function does nothing. On big endian systems,
115 * the bytes are swapped.
116 * \param x the little-endian value to convert
117 * \returns the value in host-endian byte order
119 #define libusb_le16_to_cpu libusb_cpu_to_le16
121 /* standard USB stuff */
124 * Device and/or Interface Class codes */
125 enum libusb_class_code {
126 /** In the context of a \ref libusb_device_descriptor "device descriptor",
127 * this bDeviceClass value indicates that each interface specifies its
128 * own class information and all interfaces operate independently.
130 LIBUSB_CLASS_PER_INTERFACE = 0,
133 LIBUSB_CLASS_AUDIO = 1,
135 /** Communications class */
136 LIBUSB_CLASS_COMM = 2,
138 /** Human Interface Device class */
139 LIBUSB_CLASS_HID = 3,
141 /** Printer dclass */
142 LIBUSB_CLASS_PRINTER = 7,
144 /** Picture transfer protocol class */
145 LIBUSB_CLASS_PTP = 6,
147 /** Mass storage class */
148 LIBUSB_CLASS_MASS_STORAGE = 8,
151 LIBUSB_CLASS_HUB = 9,
154 LIBUSB_CLASS_DATA = 10,
156 /** Wireless class */
157 LIBUSB_CLASS_WIRELESS = 0xe0,
159 /** Application class */
160 LIBUSB_CLASS_APPLICATION = 0xfe,
162 /** Class is vendor-specific */
163 LIBUSB_CLASS_VENDOR_SPEC = 0xff
167 * Descriptor types as defined by the USB specification. */
168 enum libusb_descriptor_type {
169 /** Device descriptor. See libusb_device_descriptor. */
170 LIBUSB_DT_DEVICE = 0x01,
172 /** Configuration descriptor. See libusb_config_descriptor. */
173 LIBUSB_DT_CONFIG = 0x02,
175 /** String descriptor */
176 LIBUSB_DT_STRING = 0x03,
178 /** Interface descriptor. See libusb_interface_descriptor. */
179 LIBUSB_DT_INTERFACE = 0x04,
181 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
182 LIBUSB_DT_ENDPOINT = 0x05,
184 /** HID descriptor */
185 LIBUSB_DT_HID = 0x21,
187 /** HID report descriptor */
188 LIBUSB_DT_REPORT = 0x22,
190 /** Physical descriptor */
191 LIBUSB_DT_PHYSICAL = 0x23,
193 /** Hub descriptor */
197 /* Descriptor sizes per descriptor type */
198 #define LIBUSB_DT_DEVICE_SIZE 18
199 #define LIBUSB_DT_CONFIG_SIZE 9
200 #define LIBUSB_DT_INTERFACE_SIZE 9
201 #define LIBUSB_DT_ENDPOINT_SIZE 7
202 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
203 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
205 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
206 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
209 * Endpoint direction. Values for bit 7 of the
210 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
212 enum libusb_endpoint_direction {
213 /** In: device-to-host */
214 LIBUSB_ENDPOINT_IN = 0x80,
216 /** Out: host-to-device */
217 LIBUSB_ENDPOINT_OUT = 0x00
220 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
223 * Endpoint transfer type. Values for bits 0:1 of the
224 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
226 enum libusb_transfer_type {
227 /** Control endpoint */
228 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
230 /** Isochronous endpoint */
231 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
234 LIBUSB_TRANSFER_TYPE_BULK = 2,
236 /** Interrupt endpoint */
237 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
241 * Standard requests, as defined in table 9-3 of the USB2 specifications */
242 enum libusb_standard_request {
243 /** Request status of the specific recipient */
244 LIBUSB_REQUEST_GET_STATUS = 0x00,
246 /** Clear or disable a specific feature */
247 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
249 /* 0x02 is reserved */
251 /** Set or enable a specific feature */
252 LIBUSB_REQUEST_SET_FEATURE = 0x03,
254 /* 0x04 is reserved */
256 /** Set device address for all future accesses */
257 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
259 /** Get the specified descriptor */
260 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
262 /** Used to update existing descriptors or add new descriptors */
263 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
265 /** Get the current device configuration value */
266 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
268 /** Set device configuration */
269 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
271 /** Return the selected alternate setting for the specified interface */
272 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
274 /** Select an alternate interface for the specified interface */
275 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
277 /** Set then report an endpoint's synchronization frame */
278 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
282 * Request type bits of the
283 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
285 enum libusb_request_type {
287 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
290 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
293 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
296 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
300 * Recipient bits of the
301 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
302 * transfers. Values 4 through 31 are reserved. */
303 enum libusb_request_recipient {
305 LIBUSB_RECIPIENT_DEVICE = 0x00,
308 LIBUSB_RECIPIENT_INTERFACE = 0x01,
311 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
314 LIBUSB_RECIPIENT_OTHER = 0x03
317 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
320 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
321 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
322 * libusb_endpoint_descriptor.
324 enum libusb_iso_sync_type {
325 /** No synchronization */
326 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
329 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
332 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
335 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
338 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
341 * Usage type for isochronous endpoints. Values for bits 4:5 of the
342 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
343 * libusb_endpoint_descriptor.
345 enum libusb_iso_usage_type {
347 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
349 /** Feedback endpoint */
350 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
352 /** Implicit feedback Data endpoint */
353 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
357 * A structure representing the standard USB device descriptor. This
358 * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
359 * All multiple-byte fields are represented in host-endian format.
361 struct libusb_device_descriptor {
362 /** Size of this descriptor (in bytes) */
365 /** Descriptor type. Will have value
366 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
368 uint8_t bDescriptorType;
370 /** USB specification release number in binary-coded decimal. A value of
371 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
374 /** USB-IF class code for the device. See \ref libusb_class_code. */
375 uint8_t bDeviceClass;
377 /** USB-IF subclass code for the device, qualified by the bDeviceClass
379 uint8_t bDeviceSubClass;
381 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
382 * bDeviceSubClass values */
383 uint8_t bDeviceProtocol;
385 /** Maximum packet size for endpoint 0 */
386 uint8_t bMaxPacketSize0;
388 /** USB-IF vendor ID */
391 /** USB-IF product ID */
394 /** Device release number in binary-coded decimal */
397 /** Index of string descriptor describing manufacturer */
398 uint8_t iManufacturer;
400 /** Index of string descriptor describing product */
403 /** Index of string descriptor containing device serial number */
404 uint8_t iSerialNumber;
406 /** Number of possible configurations */
407 uint8_t bNumConfigurations;
411 * A structure representing the standard USB endpoint descriptor. This
412 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
413 * All multiple-byte fields are represented in host-endian format.
415 struct libusb_endpoint_descriptor {
416 /** Size of this descriptor (in bytes) */
419 /** Descriptor type. Will have value
420 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
422 uint8_t bDescriptorType;
424 /** The address of the endpoint described by this descriptor. Bits 0:3 are
425 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
426 * see \ref libusb_endpoint_direction.
428 uint8_t bEndpointAddress;
430 /** Attributes which apply to the endpoint when it is configured using
431 * the bConfigurationValue. Bits 0:1 determine the transfer type and
432 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
433 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
434 * Bits 4:5 are also only used for isochronous endpoints and correspond to
435 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
437 uint8_t bmAttributes;
439 /** Maximum packet size this endpoint is capable of sending/receiving. */
440 uint16_t wMaxPacketSize;
442 /** Interval for polling endpoint for data transfers. */
445 /** For audio devices only: the rate at which synchronization feedback
449 /** For audio devices only: the address if the synch endpoint */
450 uint8_t bSynchAddress;
452 /** Extra descriptors. If libusb encounters unknown endpoint descriptors,
453 * it will store them here, should you wish to parse them. */
454 const unsigned char *extra;
456 /** Length of the extra descriptors, in bytes. */
461 * A structure representing the standard USB interface descriptor. This
462 * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
463 * All multiple-byte fields are represented in host-endian format.
465 struct libusb_interface_descriptor {
466 /** Size of this descriptor (in bytes) */
469 /** Descriptor type. Will have value
470 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
471 * in this context. */
472 uint8_t bDescriptorType;
474 /** Number of this interface */
475 uint8_t bInterfaceNumber;
477 /** Value used to select this alternate setting for this interface */
478 uint8_t bAlternateSetting;
480 /** Number of endpoints used by this interface (excluding the control
482 uint8_t bNumEndpoints;
484 /** USB-IF class code for this interface. See \ref libusb_class_code. */
485 uint8_t bInterfaceClass;
487 /** USB-IF subclass code for this interface, qualified by the
488 * bInterfaceClass value */
489 uint8_t bInterfaceSubClass;
491 /** USB-IF protocol code for this interface, qualified by the
492 * bInterfaceClass and bInterfaceSubClass values */
493 uint8_t bInterfaceProtocol;
495 /** Index of string descriptor describing this interface */
498 /** Array of endpoint descriptors. This length of this array is determined
499 * by the bNumEndpoints field. */
500 const struct libusb_endpoint_descriptor *endpoint;
502 /** Extra descriptors. If libusb encounters unknown interface descriptors,
503 * it will store them here, should you wish to parse them. */
504 const unsigned char *extra;
506 /** Length of the extra descriptors, in bytes. */
511 * A collection of alternate settings for a particular USB interface.
513 struct libusb_interface {
514 /** Array of interface descriptors. The length of this array is determined
515 * by the num_altsetting field. */
516 const struct libusb_interface_descriptor *altsetting;
518 /** The number of alternate settings that belong to this interface */
523 * A structure representing the standard USB configuration descriptor. This
524 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
525 * All multiple-byte fields are represented in host-endian format.
527 struct libusb_config_descriptor {
528 /** Size of this descriptor (in bytes) */
531 /** Descriptor type. Will have value
532 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
533 * in this context. */
534 uint8_t bDescriptorType;
536 /** Total length of data returned for this configuration */
537 uint16_t wTotalLength;
539 /** Number of interfaces supported by this configuration */
540 uint8_t bNumInterfaces;
542 /** Identifier value for this configuration */
543 uint8_t bConfigurationValue;
545 /** Index of string descriptor describing this configuration */
546 uint8_t iConfiguration;
548 /** Configuration characteristics */
549 uint8_t bmAttributes;
551 /** Maximum power consumption of the USB device from this bus in this
552 * configuration when the device is fully opreation. Expressed in units
556 /** Array of interfaces supported by this configuration. The length of
557 * this array is determined by the bNumInterfaces field. */
558 const struct libusb_interface *interface;
560 /** Extra descriptors. If libusb encounters unknown configuration
561 * descriptors, it will store them here, should you wish to parse them. */
562 const unsigned char *extra;
564 /** Length of the extra descriptors, in bytes. */
569 * Setup packet for control transfers. */
570 struct libusb_control_setup {
571 /** Request type. Bits 0:4 determine recipient, see
572 * \ref libusb_request_recipient. Bits 5:6 determine type, see
573 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
574 * \ref libusb_endpoint_direction.
576 uint8_t bmRequestType;
578 /** Request. If the type bits of bmRequestType are equal to
579 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
580 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
581 * \ref libusb_standard_request. For other cases, use of this field is
582 * application-specific. */
585 /** Value. Varies according to request */
588 /** Index. Varies according to request, typically used to pass an index
592 /** Number of bytes to transfer */
596 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
600 struct libusb_context;
601 struct libusb_device;
602 struct libusb_device_handle;
605 * Structure representing a libusb session. The concept of individual libusb
606 * sessions allows for your program to use two libraries (or dynamically
607 * load two modules) which both independently use libusb. This will prevent
608 * interference between the individual libusb users - for example
609 * libusb_set_debug() will not affect the other user of the library, and
610 * libusb_exit() will not destroy resources that the other user is still
613 * Sessions are created by libusb_init() and destroyed through libusb_exit().
614 * If your application is guaranteed to only ever include a single libusb
615 * user (i.e. you), you do not have to worry about contexts: pass NULL in
616 * every function call where a context is required. The default context
619 * For more information, see \ref contexts.
621 typedef struct libusb_context libusb_context;
624 * Structure representing a USB device detected on the system. This is an
625 * opaque type for which you are only ever provided with a pointer, usually
626 * originating from libusb_get_device_list().
628 * Certain operations can be performed on a device, but in order to do any
629 * I/O you will have to first obtain a device handle using libusb_open().
631 * Devices are reference counted with libusb_device_ref() and
632 * libusb_device_unref(), and are freed when the reference count reaches 0.
633 * New devices presented by libusb_get_device_list() have a reference count of
634 * 1, and libusb_free_device_list() can optionally decrease the reference count
635 * on all devices in the list. libusb_open() adds another reference which is
636 * later destroyed by libusb_close().
638 typedef struct libusb_device libusb_device;
642 * Structure representing a handle on a USB device. This is an opaque type for
643 * which you are only ever provided with a pointer, usually originating from
646 * A device handle is used to perform I/O and other operations. When finished
647 * with a device handle, you should call libusb_close().
649 typedef struct libusb_device_handle libusb_device_handle;
652 * Error codes. Most libusb functions return 0 on success or one of these
656 /** Success (no error) */
659 /** Input/output error */
660 LIBUSB_ERROR_IO = -1,
662 /** Invalid parameter */
663 LIBUSB_ERROR_INVALID_PARAM = -2,
665 /** Access denied (insufficient permissions) */
666 LIBUSB_ERROR_ACCESS = -3,
668 /** No such device (it may have been disconnected) */
669 LIBUSB_ERROR_NO_DEVICE = -4,
671 /** Entity not found */
672 LIBUSB_ERROR_NOT_FOUND = -5,
675 LIBUSB_ERROR_BUSY = -6,
677 /** Operation timed out */
678 LIBUSB_ERROR_TIMEOUT = -7,
681 LIBUSB_ERROR_OVERFLOW = -8,
684 LIBUSB_ERROR_PIPE = -9,
686 /** System call interrupted (perhaps due to signal) */
687 LIBUSB_ERROR_INTERRUPTED = -10,
689 /** Insufficient memory */
690 LIBUSB_ERROR_NO_MEM = -11,
692 /** Operation not supported or unimplemented on this platform */
693 LIBUSB_ERROR_NOT_SUPPORTED = -12,
696 LIBUSB_ERROR_OTHER = -99
700 * Transfer status codes */
701 enum libusb_transfer_status {
702 /** Transfer completed without error. Note that this does not indicate
703 * that the entire amount of requested data was transferred. */
704 LIBUSB_TRANSFER_COMPLETED,
706 /** Transfer failed */
707 LIBUSB_TRANSFER_ERROR,
709 /** Transfer timed out */
710 LIBUSB_TRANSFER_TIMED_OUT,
712 /** Transfer was cancelled */
713 LIBUSB_TRANSFER_CANCELLED,
715 /** For bulk/interrupt endpoints: halt condition detected (endpoint
716 * stalled). For control endpoints: control request not supported. */
717 LIBUSB_TRANSFER_STALL,
719 /** Device was disconnected */
720 LIBUSB_TRANSFER_NO_DEVICE,
722 /** Device sent more data than requested */
723 LIBUSB_TRANSFER_OVERFLOW
727 * libusb_transfer.flags values */
728 enum libusb_transfer_flags {
729 /** Report short frames as errors */
730 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
732 /** Automatically free() transfer buffer during libusb_free_transfer() */
733 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
735 /** Automatically call libusb_free_transfer() after callback returns.
736 * If this flag is set, it is illegal to call libusb_free_transfer()
737 * from your transfer callback, as this will result in a double-free
738 * when this flag is acted upon. */
739 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
743 * Isochronous packet descriptor. */
744 struct libusb_iso_packet_descriptor {
745 /** Length of data to request in this packet */
748 /** Amount of data that was actually transferred */
749 unsigned int actual_length;
751 /** Status code for this packet */
752 enum libusb_transfer_status status;
755 struct libusb_transfer;
758 * Asynchronous transfer callback function type. When submitting asynchronous
759 * transfers, you pass a pointer to a callback function of this type via the
760 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
761 * structure. libusb will call this function later, when the transfer has
762 * completed or failed. See \ref asyncio for more information.
763 * \param transfer The libusb_transfer struct the callback function is being
766 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
769 * The generic USB transfer structure. The user populates this structure and
770 * then submits it in order to request a transfer. After the transfer has
771 * completed, the library populates the transfer with the results and passes
772 * it back to the user.
774 struct libusb_transfer {
775 /** Handle of the device that this transfer will be submitted to */
776 libusb_device_handle *dev_handle;
778 /** A bitwise OR combination of \ref libusb_transfer_flags. */
781 /** Address of the endpoint where this transfer will be sent. */
782 unsigned char endpoint;
784 /** Type of the endpoint from \ref libusb_transfer_type */
787 /** Timeout for this transfer in millseconds. A value of 0 indicates no
789 unsigned int timeout;
791 /** The status of the transfer. Read-only, and only for use within
792 * transfer callback function.
794 * If this is an isochronous transfer, this field may read COMPLETED even
795 * if there were errors in the frames. Use the
796 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
797 * to determine if errors occurred. */
798 enum libusb_transfer_status status;
800 /** Length of the data buffer */
803 /** Actual length of data that was transferred. Read-only, and only for
804 * use within transfer callback function. Not valid for isochronous
805 * endpoint transfers. */
808 /** Callback function. This will be invoked when the transfer completes,
809 * fails, or is cancelled. */
810 libusb_transfer_cb_fn callback;
812 /** User context data to pass to the callback function. */
816 unsigned char *buffer;
818 /** Number of isochronous packets. Only used for I/O with isochronous
822 /** Isochronous packet descriptors, for isochronous transfers only. */
823 struct libusb_iso_packet_descriptor iso_packet_desc
824 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
825 [] /* valid C99 code */
827 [0] /* non-standard, but usually working code */
832 int LIBUSB_CALL libusb_init(libusb_context **ctx);
833 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
834 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
836 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
837 libusb_device ***list);
838 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
840 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
841 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
843 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
845 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
846 struct libusb_device_descriptor *desc);
847 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
848 struct libusb_config_descriptor **config);
849 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
850 uint8_t config_index, struct libusb_config_descriptor **config);
851 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
852 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
853 void LIBUSB_CALL libusb_free_config_descriptor(
854 struct libusb_config_descriptor *config);
855 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
856 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
857 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
858 unsigned char endpoint);
859 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
860 unsigned char endpoint);
862 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
863 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
864 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
866 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
868 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
869 int interface_number);
870 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
871 int interface_number);
873 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
874 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
876 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
877 int interface_number, int alternate_setting);
878 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
879 unsigned char endpoint);
880 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
882 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
883 int interface_number);
884 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
885 int interface_number);
886 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
887 int interface_number);
892 * Get the data section of a control transfer. This convenience function is here
893 * to remind you that the data does not start until 8 bytes into the actual
894 * buffer, as the setup packet comes first.
896 * Calling this function only makes sense from a transfer callback function,
897 * or situations where you have already allocated a suitably sized buffer at
900 * \param transfer a transfer
901 * \returns pointer to the first byte of the data section
903 static inline unsigned char *libusb_control_transfer_get_data(
904 struct libusb_transfer *transfer)
906 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
910 * Get the control setup packet of a control transfer. This convenience
911 * function is here to remind you that the control setup occupies the first
912 * 8 bytes of the transfer data buffer.
914 * Calling this function only makes sense from a transfer callback function,
915 * or situations where you have already allocated a suitably sized buffer at
918 * \param transfer a transfer
919 * \returns a casted pointer to the start of the transfer data buffer
921 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
922 struct libusb_transfer *transfer)
924 return (struct libusb_control_setup *) transfer->buffer;
928 * Helper function to populate the setup packet (first 8 bytes of the data
929 * buffer) for a control transfer. The wIndex, wValue and wLength values should
930 * be given in host-endian byte order.
932 * \param buffer buffer to output the setup packet into
933 * \param bmRequestType see the
934 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
935 * \ref libusb_control_setup
936 * \param bRequest see the
937 * \ref libusb_control_setup::bRequest "bRequest" field of
938 * \ref libusb_control_setup
939 * \param wValue see the
940 * \ref libusb_control_setup::wValue "wValue" field of
941 * \ref libusb_control_setup
942 * \param wIndex see the
943 * \ref libusb_control_setup::wIndex "wIndex" field of
944 * \ref libusb_control_setup
945 * \param wLength see the
946 * \ref libusb_control_setup::wLength "wLength" field of
947 * \ref libusb_control_setup
949 static inline void libusb_fill_control_setup(unsigned char *buffer,
950 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
953 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
954 setup->bmRequestType = bmRequestType;
955 setup->bRequest = bRequest;
956 setup->wValue = libusb_cpu_to_le16(wValue);
957 setup->wIndex = libusb_cpu_to_le16(wIndex);
958 setup->wLength = libusb_cpu_to_le16(wLength);
961 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
962 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
963 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
964 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
967 * Helper function to populate the required \ref libusb_transfer fields
968 * for a control transfer.
970 * If you pass a transfer buffer to this function, the first 8 bytes will
971 * be interpreted as a control setup packet, and the wLength field will be
972 * used to automatically populate the \ref libusb_transfer::length "length"
973 * field of the transfer. Therefore the recommended approach is:
974 * -# Allocate a suitably sized data buffer (including space for control setup)
975 * -# Call libusb_fill_control_setup()
976 * -# If this is a host-to-device transfer with a data stage, put the data
977 * in place after the setup packet
978 * -# Call this function
979 * -# Call libusb_submit_transfer()
981 * It is also legal to pass a NULL buffer to this function, in which case this
982 * function will not attempt to populate the length field. Remember that you
983 * must then populate the buffer and length fields later.
985 * \param transfer the transfer to populate
986 * \param dev_handle handle of the device that will handle the transfer
987 * \param buffer data buffer. If provided, this function will interpret the
988 * first 8 bytes as a setup packet and infer the transfer length from that.
989 * \param callback callback function to be invoked on transfer completion
990 * \param user_data user data to pass to callback function
991 * \param timeout timeout for the transfer in milliseconds
993 static inline void libusb_fill_control_transfer(
994 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
995 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
996 unsigned int timeout)
998 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
999 transfer->dev_handle = dev_handle;
1000 transfer->endpoint = 0;
1001 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1002 transfer->timeout = timeout;
1003 transfer->buffer = buffer;
1005 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1006 + libusb_le16_to_cpu(setup->wLength);
1007 transfer->user_data = user_data;
1008 transfer->callback = callback;
1011 /** \ingroup asyncio
1012 * Helper function to populate the required \ref libusb_transfer fields
1013 * for a bulk transfer.
1015 * \param transfer the transfer to populate
1016 * \param dev_handle handle of the device that will handle the transfer
1017 * \param endpoint address of the endpoint where this transfer will be sent
1018 * \param buffer data buffer
1019 * \param length length of data buffer
1020 * \param callback callback function to be invoked on transfer completion
1021 * \param user_data user data to pass to callback function
1022 * \param timeout timeout for the transfer in milliseconds
1024 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1025 libusb_device_handle *dev_handle, unsigned char endpoint,
1026 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1027 void *user_data, unsigned int timeout)
1029 transfer->dev_handle = dev_handle;
1030 transfer->endpoint = endpoint;
1031 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1032 transfer->timeout = timeout;
1033 transfer->buffer = buffer;
1034 transfer->length = length;
1035 transfer->user_data = user_data;
1036 transfer->callback = callback;
1039 /** \ingroup asyncio
1040 * Helper function to populate the required \ref libusb_transfer fields
1041 * for an interrupt transfer.
1043 * \param transfer the transfer to populate
1044 * \param dev_handle handle of the device that will handle the transfer
1045 * \param endpoint address of the endpoint where this transfer will be sent
1046 * \param buffer data buffer
1047 * \param length length of data buffer
1048 * \param callback callback function to be invoked on transfer completion
1049 * \param user_data user data to pass to callback function
1050 * \param timeout timeout for the transfer in milliseconds
1052 static inline void libusb_fill_interrupt_transfer(
1053 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1054 unsigned char endpoint, unsigned char *buffer, int length,
1055 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1057 transfer->dev_handle = dev_handle;
1058 transfer->endpoint = endpoint;
1059 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1060 transfer->timeout = timeout;
1061 transfer->buffer = buffer;
1062 transfer->length = length;
1063 transfer->user_data = user_data;
1064 transfer->callback = callback;
1067 /** \ingroup asyncio
1068 * Helper function to populate the required \ref libusb_transfer fields
1069 * for an isochronous transfer.
1071 * \param transfer the transfer to populate
1072 * \param dev_handle handle of the device that will handle the transfer
1073 * \param endpoint address of the endpoint where this transfer will be sent
1074 * \param buffer data buffer
1075 * \param length length of data buffer
1076 * \param num_iso_packets the number of isochronous packets
1077 * \param callback callback function to be invoked on transfer completion
1078 * \param user_data user data to pass to callback function
1079 * \param timeout timeout for the transfer in milliseconds
1081 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1082 libusb_device_handle *dev_handle, unsigned char endpoint,
1083 unsigned char *buffer, int length, int num_iso_packets,
1084 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1086 transfer->dev_handle = dev_handle;
1087 transfer->endpoint = endpoint;
1088 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1089 transfer->timeout = timeout;
1090 transfer->buffer = buffer;
1091 transfer->length = length;
1092 transfer->num_iso_packets = num_iso_packets;
1093 transfer->user_data = user_data;
1094 transfer->callback = callback;
1097 /** \ingroup asyncio
1098 * Convenience function to set the length of all packets in an isochronous
1099 * transfer, based on the num_iso_packets field in the transfer structure.
1101 * \param transfer a transfer
1102 * \param length the length to set in each isochronous packet descriptor
1103 * \see libusb_get_max_packet_size()
1105 static inline void libusb_set_iso_packet_lengths(
1106 struct libusb_transfer *transfer, unsigned int length)
1109 for (i = 0; i < transfer->num_iso_packets; i++)
1110 transfer->iso_packet_desc[i].length = length;
1113 /** \ingroup asyncio
1114 * Convenience function to locate the position of an isochronous packet
1115 * within the buffer of an isochronous transfer.
1117 * This is a thorough function which loops through all preceding packets,
1118 * accumulating their lengths to find the position of the specified packet.
1119 * Typically you will assign equal lengths to each packet in the transfer,
1120 * and hence the above method is sub-optimal. You may wish to use
1121 * libusb_get_iso_packet_buffer_simple() instead.
1123 * \param transfer a transfer
1124 * \param packet the packet to return the address of
1125 * \returns the base address of the packet buffer inside the transfer buffer,
1126 * or NULL if the packet does not exist.
1127 * \see libusb_get_iso_packet_buffer_simple()
1129 static inline unsigned char *libusb_get_iso_packet_buffer(
1130 struct libusb_transfer *transfer, unsigned int packet)
1136 /* oops..slight bug in the API. packet is an unsigned int, but we use
1137 * signed integers almost everywhere else. range-check and convert to
1138 * signed to avoid compiler warnings. FIXME for libusb-2. */
1139 if (packet > INT_MAX)
1143 if (_packet >= transfer->num_iso_packets)
1146 for (i = 0; i < _packet; i++)
1147 offset += transfer->iso_packet_desc[i].length;
1149 return transfer->buffer + offset;
1152 /** \ingroup asyncio
1153 * Convenience function to locate the position of an isochronous packet
1154 * within the buffer of an isochronous transfer, for transfers where each
1155 * packet is of identical size.
1157 * This function relies on the assumption that every packet within the transfer
1158 * is of identical size to the first packet. Calculating the location of
1159 * the packet buffer is then just a simple calculation:
1160 * <tt>buffer + (packet_size * packet)</tt>
1162 * Do not use this function on transfers other than those that have identical
1163 * packet lengths for each packet.
1165 * \param transfer a transfer
1166 * \param packet the packet to return the address of
1167 * \returns the base address of the packet buffer inside the transfer buffer,
1168 * or NULL if the packet does not exist.
1169 * \see libusb_get_iso_packet_buffer()
1171 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1172 struct libusb_transfer *transfer, unsigned int packet)
1176 /* oops..slight bug in the API. packet is an unsigned int, but we use
1177 * signed integers almost everywhere else. range-check and convert to
1178 * signed to avoid compiler warnings. FIXME for libusb-2. */
1179 if (packet > INT_MAX)
1183 if (_packet >= transfer->num_iso_packets)
1186 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1191 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1192 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1193 unsigned char *data, uint16_t wLength, unsigned int timeout);
1195 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1196 unsigned char endpoint, unsigned char *data, int length,
1197 int *actual_length, unsigned int timeout);
1199 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1200 unsigned char endpoint, unsigned char *data, int length,
1201 int *actual_length, unsigned int timeout);
1204 * Retrieve a descriptor from the default control pipe.
1205 * This is a convenience function which formulates the appropriate control
1206 * message to retrieve the descriptor.
1208 * \param dev a device handle
1209 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1210 * \param desc_index the index of the descriptor to retrieve
1211 * \param data output buffer for descriptor
1212 * \param length size of data buffer
1213 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1215 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1216 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1218 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1219 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1220 (uint16_t) length, 1000);
1224 * Retrieve a descriptor from a device.
1225 * This is a convenience function which formulates the appropriate control
1226 * message to retrieve the descriptor. The string returned is Unicode, as
1227 * detailed in the USB specifications.
1229 * \param dev a device handle
1230 * \param desc_index the index of the descriptor to retrieve
1231 * \param langid the language ID for the string descriptor
1232 * \param data output buffer for descriptor
1233 * \param length size of data buffer
1234 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1235 * \see libusb_get_string_descriptor_ascii()
1237 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1238 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1240 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1241 LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
1242 langid, data, (uint16_t) length, 1000);
1245 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1246 uint8_t desc_index, unsigned char *data, int length);
1248 /* polling and timeouts */
1250 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1251 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1252 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1253 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1254 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1255 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1256 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1257 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1259 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1260 struct timeval *tv);
1261 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1262 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1263 struct timeval *tv);
1264 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1265 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1266 struct timeval *tv);
1269 * File descriptor for polling
1271 struct libusb_pollfd {
1272 /** Numeric file descriptor */
1275 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1276 * should monitor this file descriptor for becoming ready to read from,
1277 * and POLLOUT indicates that you should monitor this file descriptor for
1278 * nonblocking write readiness. */
1283 * Callback function, invoked when a new file descriptor should be added
1284 * to the set of file descriptors monitored for events.
1285 * \param fd the new file descriptor
1286 * \param events events to monitor for, see \ref libusb_pollfd for a
1288 * \param user_data User data pointer specified in
1289 * libusb_set_pollfd_notifiers() call
1290 * \see libusb_set_pollfd_notifiers()
1292 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1296 * Callback function, invoked when a file descriptor should be removed from
1297 * the set of file descriptors being monitored for events. After returning
1298 * from this callback, do not use that file descriptor again.
1299 * \param fd the file descriptor to stop monitoring
1300 * \param user_data User data pointer specified in
1301 * libusb_set_pollfd_notifiers() call
1302 * \see libusb_set_pollfd_notifiers()
1304 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1306 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1307 libusb_context *ctx);
1308 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1309 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,