| libusb 1.0.24 A cross-platform user library to access USB devices

Data Structures | Typedefs | Enumerations | Functions
Asynchronous device I/O

This page details libusb's asynchronous (non-blocking) API for USB device I/O. More...

## Data Structures
struct | libusb_control_setup
| Setup packet for control transfers. More...
struct | libusb_iso_packet_descriptor
| Isochronous packet descriptor. More...
struct | libusb_transfer
| The generic USB transfer structure. More...

## Typedefs
typedef void(* | libusb_transfer_cb_fn) (struct libusb_transfer *transfer)
| Asynchronous transfer callback function type. More...

## Enumerations
enum | libusb_transfer_type { LIBUSB_TRANSFER_TYPE_CONTROL = 0U , LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1U , LIBUSB_TRANSFER_TYPE_BULK = 2U , LIBUSB_TRANSFER_TYPE_INTERRUPT = 3U , LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4U }
| Transfer type. More...
enum | libusb_transfer_status { LIBUSB_TRANSFER_COMPLETED , LIBUSB_TRANSFER_ERROR , LIBUSB_TRANSFER_TIMED_OUT , LIBUSB_TRANSFER_CANCELLED , LIBUSB_TRANSFER_STALL , LIBUSB_TRANSFER_NO_DEVICE , LIBUSB_TRANSFER_OVERFLOW }
| Transfer status codes. More...
enum | libusb_transfer_flags { LIBUSB_TRANSFER_SHORT_NOT_OK = (1U << 0) , LIBUSB_TRANSFER_FREE_BUFFER = (1U << 1) , LIBUSB_TRANSFER_FREE_TRANSFER = (1U << 2) , LIBUSB_TRANSFER_ADD_ZERO_PACKET = (1U << 3) }
| libusb_transfer.flags values More...

## Functions
int | libusb_alloc_streams (libusb_device_handle *dev_handle, uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
| Allocate up to num_streams usb bulk streams on the specified endpoints. More...
int | libusb_free_streams (libusb_device_handle *dev_handle, unsigned char *endpoints, int num_endpoints)
| Free usb bulk streams allocated with libusb_alloc_streams(). More...
unsigned char * | libusb_dev_mem_alloc (libusb_device_handle *dev_handle, size_t length)
| Attempts to allocate a block of persistent DMA memory suitable for transfers against the given device. More...
int | libusb_dev_mem_free (libusb_device_handle *dev_handle, unsigned char *buffer, size_t length)
| Free device memory allocated with libusb_dev_mem_alloc(). More...
struct libusb_transfer * | libusb_alloc_transfer (int iso_packets)
| Allocate a libusb transfer with a specified number of isochronous packet descriptors. More...
void | libusb_free_transfer (struct libusb_transfer *transfer)
| Free a transfer structure. More...
int | libusb_submit_transfer (struct libusb_transfer *transfer)
| Submit a transfer. More...
int | libusb_cancel_transfer (struct libusb_transfer *transfer)
| Asynchronously cancel a previously submitted transfer. More...
void | libusb_transfer_set_stream_id (struct libusb_transfer *transfer, uint32_t stream_id)
| Set a transfers bulk stream id. More...
uint32_t | libusb_transfer_get_stream_id (struct libusb_transfer *transfer)
| Get a transfers bulk stream id. More...
static unsigned char * | libusb_control_transfer_get_data (struct libusb_transfer *transfer)
| Get the data section of a control transfer. More...
static struct libusb_control_setup * | libusb_control_transfer_get_setup (struct libusb_transfer *transfer)
| Get the control setup packet of a control transfer. More...
static void | libusb_fill_control_setup (unsigned char *buffer, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
| Helper function to populate the setup packet (first 8 bytes of the data buffer) for a control transfer. More...
static void | libusb_fill_control_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
| Helper function to populate the required libusb_transfer fields for a control transfer. More...
static void | libusb_fill_bulk_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
| Helper function to populate the required libusb_transfer fields for a bulk transfer. More...
static void | libusb_fill_bulk_stream_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, uint32_t stream_id, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
| Helper function to populate the required libusb_transfer fields for a bulk transfer using bulk streams. More...
static void | libusb_fill_interrupt_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
| Helper function to populate the required libusb_transfer fields for an interrupt transfer. More...
static void | libusb_fill_iso_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
| Helper function to populate the required libusb_transfer fields for an isochronous transfer. More...
static void | libusb_set_iso_packet_lengths (struct libusb_transfer *transfer, unsigned int length)
| Convenience function to set the length of all packets in an isochronous transfer, based on the num_iso_packets field in the transfer structure. More...
static unsigned char * | libusb_get_iso_packet_buffer (struct libusb_transfer *transfer, unsigned int packet)
| Convenience function to locate the position of an isochronous packet within the buffer of an isochronous transfer. More...
static unsigned char * | libusb_get_iso_packet_buffer_simple (struct libusb_transfer *transfer, unsigned int packet)
| Convenience function to locate the position of an isochronous packet within the buffer of an isochronous transfer, for transfers where each packet is of identical size. More...

## Detailed Description

This page details libusb's asynchronous (non-blocking) API for USB device I/O.

This interface is very powerful but is also quite complex - you will need to read this page carefully to understand the necessary considerations and issues surrounding use of this interface. Simplistic applications may wish to consider the synchronous I/O API instead.

The asynchronous interface is built around the idea of separating transfer submission and handling of transfer completion (the synchronous model combines both of these into one). There may be a long delay between submission and completion, however the asynchronous submission function is non-blocking so will return control to your application during that potentially long delay.

# Transfer abstraction

For the asynchronous I/O, libusb implements the concept of a generic transfer entity for all types of I/O (control, bulk, interrupt, isochronous). The generic transfer object must be treated slightly differently depending on which type of I/O you are performing with it.

This is represented by the public libusb_transfer structure type.

# Asynchronous transfers

We can view asynchronous I/O as a 5 step process:

1. Allocation: allocate a libusb_transfer
2. Filling: populate the libusb_transfer instance with information about the transfer you wish to perform
3. Submission: ask libusb to submit the transfer
4. Completion handling: examine transfer results in the libusb_transfer structure
5. Deallocation: clean up resources

## Allocation

This step involves allocating memory for a USB transfer. This is the generic transfer object mentioned above. At this stage, the transfer is "blank" with no details about what type of I/O it will be used for.

Allocation is done with the libusb_alloc_transfer() function. You must use this function rather than allocating your own transfers.

## Filling

This step is where you take a previously allocated transfer and fill it with information to determine the message type and direction, data buffer, callback function, etc.

You can either fill the required fields yourself or you can use the helper functions: libusb_fill_control_transfer(), libusb_fill_bulk_transfer() and libusb_fill_interrupt_transfer().

## Submission

When you have allocated a transfer and filled it, you can submit it using libusb_submit_transfer(). This function returns immediately but can be regarded as firing off the I/O request in the background.

## Completion handling

After a transfer has been submitted, one of four things can happen to it:

- The transfer completes (i.e. some data was transferred)
- The transfer has a timeout and the timeout expires before all data is transferred
- The transfer fails due to an error
- The transfer is cancelled

Each of these will cause the user-specified transfer callback function to be invoked. It is up to the callback function to determine which of the above actually happened and to act accordingly.

The user-specified callback is passed a pointer to the libusb_transfer structure which was used to setup and submit the transfer. At completion time, libusb has populated this structure with results of the transfer: success or failure reason, number of bytes of data transferred, etc. See the libusb_transfer structure documentation for more information.

Important Note: The user-specified callback is called from an event handling context. It is therefore important that no calls are made into libusb that will attempt to perform any event handling. Examples of such functions are any listed in the synchronous API and any of the blocking functions that retrieve USB descriptors.

## Deallocation

When a transfer has completed (i.e. the callback function has been invoked), you are advised to free the transfer (unless you wish to resubmit it, see below). Transfers are deallocated with libusb_free_transfer().

It is undefined behaviour to free a transfer which has not completed.

# Resubmission

You may be wondering why allocation, filling, and submission are all separated above where they could reasonably be combined into a single operation.

The reason for separation is to allow you to resubmit transfers without having to allocate new ones every time. This is especially useful for common situations dealing with interrupt endpoints - you allocate one transfer, fill and submit it, and when it returns with results you just resubmit it for the next interrupt.

# Cancellation

Another advantage of using the asynchronous interface is that you have the ability to cancel transfers which have not yet completed. This is done by calling the libusb_cancel_transfer() function.

libusb_cancel_transfer() is asynchronous/non-blocking in itself. When the cancellation actually completes, the transfer's callback function will be invoked, and the callback function should check the transfer status to determine that it was cancelled.

Freeing the transfer after it has been cancelled but before cancellation has completed will result in undefined behaviour.

Attention
  When a transfer is cancelled, some of the data may have been transferred. libusb will communicate this to you in the transfer callback. Do not assume that no data was transferred.

# Partial data transfer resulting from cancellation

As noted above, some of the data may have been transferred at the time a transfer is cancelled. It is helpful to see how this is possible if you consider a bulk transfer to an endpoint with a packet size of 64 bytes. Supposing you submit a 512-byte transfer to this endpoint, the operating system will divide this transfer up into 8 separate 64-byte frames that the host controller will schedule for the device to transfer data. If this transfer is cancelled while the device is transferring data, a subset of these frames may be descheduled from the host controller before the device has the opportunity to finish transferring data to the host.

What your application should do with a partial data transfer is a policy decision; there is no single answer that satisfies the needs of every application. The data that was successfully transferred should be considered entirely valid, but your application must decide what to do with the remaining data that was not transferred. Some possible actions to take are:

- Resubmit another transfer for the remaining data, possibly with a shorter timeout
- Discard the partially transferred data and report an error

# Timeouts

When a transfer times out, libusb internally notes this and attempts to cancel the transfer. As noted in above, it is possible that some of the data may actually have been transferred. Your application should always check how much data was actually transferred once the transfer completes and act accordingly.

# Overflows on device-to-host bulk/interrupt endpoints

If your device does not have predictable transfer sizes (or it misbehaves), your application may submit a request for data on an IN endpoint which is smaller than the data that the device wishes to send. In some circumstances this will cause an overflow, which is a nasty condition to deal with. See the Packets and overflows page for discussion.

# Considerations for control transfers

The `libusb_transfer` structure is generic and hence does not include specific fields for the control-specific setup packet structure.

In order to perform a control transfer, you must place the 8-byte setup packet at the start of the data buffer. To simplify this, you could cast the buffer pointer to type struct libusb_control_setup, or you can use the helper function libusb_fill_control_setup().

The wLength field placed in the setup packet must be the length you would expect to be sent in the setup packet: the length of the payload that follows (or the expected maximum number of bytes to receive). However, the length field of the libusb_transfer object must be the length of the data buffer - i.e. it should be wLength plus the size of the setup packet (LIBUSB_CONTROL_SETUP_SIZE).

If you use the helper functions, this is simplified for you:

1. Allocate a buffer of size LIBUSB_CONTROL_SETUP_SIZE plus the size of the data you are sending/requesting.
2. Call libusb_fill_control_setup() on the data buffer, using the transfer request size as the wLength value (i.e. do not include the extra space you allocated for the control setup).
3. If this is a host-to-device transfer, place the data to be transferred in the data buffer, starting at offset LIBUSB_CONTROL_SETUP_SIZE.
4. Call libusb_fill_control_transfer() to associate the data buffer with the transfer (and to set the remaining details such as callback and timeout).
   - Note that there is no parameter to set the length field of the transfer. The length is automatically inferred from the wLength field of the setup packet.
5. Submit the transfer.

The multi-byte control setup fields (wValue, wIndex and wLength) must be given in little-endian byte order (the endianness of the USB bus). Endianness conversion is transparently handled by libusb_fill_control_setup() which is documented to accept host-endian values.

Further considerations are needed when handling transfer completion in your callback function:

- As you might expect, the setup packet will still be sitting at the start of the data buffer.
- If this was a device-to-host transfer, the received data will be sitting at offset LIBUSB_CONTROL_SETUP_SIZE into the buffer.
- The actual_length field of the transfer structure is relative to the wLength of the setup packet, rather than the size of the data buffer. So, if your wLength was 4, your transfer's `length` was 12, then you should expect an `actual_length` of 4 to indicate that the data was transferred in entirety.

To simplify parsing of setup packets and obtaining the data from the correct offset, you may wish to use the libusb_control_transfer_get_data() and libusb_control_transfer_get_setup() functions within your transfer callback.

Even though control endpoints do not halt, a completed control transfer may have a LIBUSB_TRANSFER_STALL status code. This indicates the control request was not supported.

# Considerations for interrupt transfers

All interrupt transfers are performed using the polling interval presented by the bInterval value of the endpoint descriptor.

# Considerations for isochronous transfers

Isochronous transfers are more complicated than transfers to non-isochronous endpoints.

To perform I/O to an isochronous endpoint, allocate the transfer by calling libusb_alloc_transfer() with an appropriate number of isochronous packets.

During filling, set type to LIBUSB_TRANSFER_TYPE_ISOCHRONOUS, and set num_iso_packets to a value less than or equal to the number of packets you requested during allocation. libusb_alloc_transfer() does not set either of these fields for you, given that you might not even use the transfer on an isochronous endpoint.

Next, populate the length field for the first num_iso_packets entries in the iso_packet_desc array. Section 5.6.3 of the USB2 specifications describe how the maximum isochronous packet length is determined by the wMaxPacketSize field in the endpoint descriptor. Two functions can help you here:

- libusb_get_max_iso_packet_size() is an easy way to determine the max packet size for an isochronous endpoint. Note that the maximum packet size is actually the maximum number of bytes that can be transmitted in a single microframe, therefore this function multiplies the maximum number of bytes per transaction by the number of transaction opportunities per microframe.
- libusb_set_iso_packet_lengths() assigns the same length to all packets within a transfer, which is usually what you want.

For outgoing transfers, you'll obviously fill the buffer and populate the packet descriptors in hope that all the data gets transferred. For incoming transfers, you must ensure the buffer has sufficient capacity for the situation where all packets transfer the full amount of requested data.

Completion handling requires some extra consideration. The actual_length field of the transfer is meaningless and should not be examined; instead you must refer to the actual_length field of each individual packet.

The status field of the transfer is also a little misleading:

- If the packets were submitted and the isochronous data microframes completed normally, status will have value LIBUSB_TRANSFER_COMPLETED. Note that bus errors and software-incurred delays are not counted as transfer errors; the transfer.status field may indicate COMPLETED even if some or all of the packets failed. Refer to the status field of each individual packet to determine packet failures.
- The status field will have value LIBUSB_TRANSFER_ERROR only when serious errors were encountered.
- Other transfer status codes occur with normal behaviour.

The data for each packet will be found at an offset into the buffer that can be calculated as if each prior packet completed in full. The libusb_get_iso_packet_buffer() and libusb_get_iso_packet_buffer_simple() functions may help you here.

# Transfer length limitations

Some operating systems may impose limits on the length of the transfer data buffer or, in the case of isochronous transfers, the length of individual isochronous packets. Such limits can be difficult for libusb to detect, so in most cases the library will simply try and submit the transfer as set up by you. If the transfer fails to submit because it is too large, libusb_submit_transfer() will return LIBUSB_ERROR_INVALID_PARAM.

The following are known limits for control transfer lengths. Note that this length includes the 8-byte setup packet.

- Linux (4,096 bytes)
- Windows (4,096 bytes)

# Memory caveats

In most circumstances, it is not safe to use stack memory for transfer buffers. This is because the function that fired off the asynchronous transfer may return before libusb has finished using the buffer, and when the function returns it's stack gets destroyed. This is true for both host-to-device and device-to-host transfers.

The only case in which it is safe to use stack memory is where you can guarantee that the function owning the stack space for the buffer does not return until after the transfer's callback function has completed. In every other case, you need to use heap memory instead.

# Fine control

Through using this asynchronous interface, you may find yourself repeating a few simple operations many times. You can apply a bitwise OR of certain flags to a transfer to simplify certain things:

- LIBUSB_TRANSFER_SHORT_NOT_OK results in transfers which transferred less than the requested amount of data being marked with status LIBUSB_TRANSFER_ERROR (they would normally be regarded as COMPLETED)
- LIBUSB_TRANSFER_FREE_BUFFER allows you to ask libusb to free the transfer buffer when freeing the transfer.
- LIBUSB_TRANSFER_FREE_TRANSFER causes libusb to automatically free the transfer after the transfer callback returns.

# Event handling

An asynchronous model requires that libusb perform work at various points in time - namely processing the results of previously-submitted transfers and invoking the user-supplied callback function.

This gives rise to the libusb_handle_events() function which your application must call into when libusb has work do to. This gives libusb the opportunity to reap pending transfers, invoke callbacks, etc.

Note
  All event handling is performed by whichever thread calls the libusb_handle_events() function. libusb does not invoke any callbacks outside of this context. Consequently, any callbacks will be run on the thread that calls the libusb_handle_events() function.

When to call the libusb_handle_events() function depends on which model your application decides to use. The 2 different approaches:

1. Repeatedly call libusb_handle_events() in blocking mode from a dedicated thread.
2. Integrate libusb with your application's main event loop. libusb exposes a set of file descriptors which allow you to do this.

The first approach has the big advantage that it will also work on Windows were libusb' poll API for select / poll integration is not available. So if you want to support Windows and use the async API, you must use this approach, see the Using an event handling thread section below for details.

If you prefer a single threaded approach with a single central event loop, see the polling and timing section for how to integrate libusb into your application's main event loop.

# Using an event handling thread

Lets begin with stating the obvious: If you're going to use a separate thread for libusb event handling, your callback functions MUST be thread-safe.

Other then that doing event handling from a separate thread, is mostly simple. You can use an event thread function as follows:

void *event_thread_func(void *ctx)
{
while (event_thread_run)
libusb_handle_events(ctx);
return NULL;
}
libusb_handle_events
int libusb_handle_events(libusb_context *ctx)
Handle any pending events in blocking mode.
Definition: io.c:2411

There is one caveat though, stopping this thread requires setting the event_thread_run variable to 0, and after that libusb_handle_events() needs to return control to event_thread_func. But unless some event happens, libusb_handle_events() will not return.

There are 2 different ways of dealing with this, depending on if your application uses libusb' hotplug support or not.

Applications which do not use hotplug support, should not start the event thread until after their first call to libusb_open(), and should stop the thread when closing the last open device as follows:

void my_close_handle(libusb_device_handle *dev_handle)
{
if (open_devs == 1)
event_thread_run = 0;
libusb_close(dev_handle); // This wakes up libusb_handle_events()
if (open_devs == 1)
pthread_join(event_thread);
open_devs--;
}
libusb_close
void libusb_close(libusb_device_handle *dev_handle)
Close a device handle.
Definition: core.c:1457
libusb_device_handle
struct libusb_device_handle libusb_device_handle
Structure representing a handle on a USB device.
Definition: libusb.h:1015

Applications using hotplug support should start the thread at program init, after having successfully called libusb_hotplug_register_callback(), and should stop the thread at program exit as follows:

void my_libusb_exit(void)
{
event_thread_run = 0;
libusb_hotplug_deregister_callback(ctx, hotplug_cb_handle); // This wakes up libusb_handle_events()
pthread_join(event_thread);
libusb_exit(ctx);
}
libusb_hotplug_deregister_callback
void libusb_hotplug_deregister_callback(libusb_context *ctx, libusb_hotplug_callback_handle callback_handle)
Deregisters a hotplug callback.
Definition: hotplug.c:319
libusb_exit
void libusb_exit(libusb_context *ctx)
Deinitialize libusb.
Definition: core.c:2367

## Typedef Documentation

## ◆ libusb_transfer_cb_fn

typedef void(* libusb_transfer_cb_fn) (struct libusb_transfer *transfer)

Asynchronous transfer callback function type.

When submitting asynchronous transfers, you pass a pointer to a callback function of this type via the callback member of the libusb_transfer structure. libusb will call this function later, when the transfer has completed or failed. See Asynchronous device I/O for more information.

Parameters

  transfer | The libusb_transfer struct the callback function is being notified about.

## Enumeration Type Documentation

## ◆ libusb_transfer_type

enum libusb_transfer_type

Transfer type.

Enumerator
LIBUSB_TRANSFER_TYPE_CONTROL | Control transfer.
LIBUSB_TRANSFER_TYPE_ISOCHRONOUS | Isochronous transfer.
LIBUSB_TRANSFER_TYPE_BULK | Bulk transfer.
LIBUSB_TRANSFER_TYPE_INTERRUPT | Interrupt transfer.
LIBUSB_TRANSFER_TYPE_BULK_STREAM | Bulk stream transfer.

## ◆ libusb_transfer_status

enum libusb_transfer_status

Transfer status codes.

Enumerator
LIBUSB_TRANSFER_COMPLETED | Transfer completed without error. Note that this does not indicate that the entire amount of requested data was transferred.
LIBUSB_TRANSFER_ERROR | Transfer failed.
LIBUSB_TRANSFER_TIMED_OUT | Transfer timed out.
LIBUSB_TRANSFER_CANCELLED | Transfer was cancelled.
LIBUSB_TRANSFER_STALL | For bulk/interrupt endpoints: halt condition detected (endpoint stalled). For control endpoints: control request not supported.
LIBUSB_TRANSFER_NO_DEVICE | Device was disconnected.
LIBUSB_TRANSFER_OVERFLOW | Device sent more data than requested.

## ◆ libusb_transfer_flags

enum libusb_transfer_flags

libusb_transfer.flags values

Enumerator
LIBUSB_TRANSFER_SHORT_NOT_OK | Report short frames as errors.
LIBUSB_TRANSFER_FREE_BUFFER | Automatically free() transfer buffer during libusb_free_transfer(). Note that buffers allocated with libusb_dev_mem_alloc() should not be attempted freed in this way, since free() is not an appropriate way to release such memory.
LIBUSB_TRANSFER_FREE_TRANSFER | Automatically call libusb_free_transfer() after callback returns. If this flag is set, it is illegal to call libusb_free_transfer() from your transfer callback, as this will result in a double-free when this flag is acted upon.
LIBUSB_TRANSFER_ADD_ZERO_PACKET | Terminate transfers that are a multiple of the endpoint's wMaxPacketSize with an extra zero length packet. This is useful when a device protocol mandates that each logical request is terminated by an incomplete packet (i.e. the logical requests are not separated by other means). This flag only affects host-to-device transfers to bulk and interrupt endpoints. In other situations, it is ignored. This flag only affects transfers with a length that is a multiple of the endpoint's wMaxPacketSize. On transfers of other lengths, this flag has no effect. Therefore, if you are working with a device that needs a ZLP whenever the end of the logical request falls on a packet boundary, then it is sensible to set this flag on every transfer (you do not have to worry about only setting it on transfers that end on the boundary). This flag is currently only supported on Linux. On other systems, libusb_submit_transfer() will return LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set. Available since libusb-1.0.9.

## Function Documentation

## ◆ libusb_alloc_streams()

int libusb_alloc_streams | ( | libusb_device_handle * | dev_handle,
|  | uint32_t | num_streams,
|  | unsigned char * | endpoints,
|  | int | num_endpoints
| ) |  |

Allocate up to num_streams usb bulk streams on the specified endpoints.

This function takes an array of endpoints rather then a single endpoint because some protocols require that endpoints are setup with similar stream ids. All endpoints passed in must belong to the same interface.

Note this function may return less streams then requested. Also note that the same number of streams are allocated for each endpoint in the endpoint array.

Stream id 0 is reserved, and should not be used to communicate with devices. If libusb_alloc_streams() returns with a value of N, you may use stream ids 1 to N.

Since version 1.0.19, LIBUSB_API_VERSION >= 0x01000103

Parameters

  dev_handle | a device handle
  num_streams | number of streams to try to allocate
  endpoints | array of endpoints to allocate streams on
  num_endpoints | length of the endpoints array

Returns
  number of streams allocated, or a LIBUSB_ERROR code on failure

## ◆ libusb_free_streams()

int libusb_free_streams | ( | libusb_device_handle * | dev_handle,
|  | unsigned char * | endpoints,
|  | int | num_endpoints
| ) |  |

Free usb bulk streams allocated with libusb_alloc_streams().

Note streams are automatically free-ed when releasing an interface.

Since version 1.0.19, LIBUSB_API_VERSION >= 0x01000103

Parameters

  dev_handle | a device handle
  endpoints | array of endpoints to free streams on
  num_endpoints | length of the endpoints array

Returns
  LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure

## ◆ libusb_dev_mem_alloc()

unsigned char * libusb_dev_mem_alloc | ( | libusb_device_handle * | dev_handle,
|  | size_t | length
| ) |  |

Attempts to allocate a block of persistent DMA memory suitable for transfers against the given device.

If successful, will return a block of memory that is suitable for use as "buffer" in libusb_transfer against this device. Using this memory instead of regular memory means that the host controller can use DMA directly into the buffer to increase performance, and also that transfers can no longer fail due to kernel memory fragmentation.

Note that this means you should not modify this memory (or even data on the same cache lines) when a transfer is in progress, although it is legal to have several transfers going on within the same memory block.

Will return NULL on failure. Many systems do not support such zero-copy and will always return NULL. Memory allocated with this function must be freed with libusb_dev_mem_free. Specifically, this means that the flag LIBUSB_TRANSFER_FREE_BUFFER cannot be used to free memory allocated with this function.

Since version 1.0.21, LIBUSB_API_VERSION >= 0x01000105

Parameters

  dev_handle | a device handle
  length | size of desired data buffer

Returns
  a pointer to the newly allocated memory, or NULL on failure

## ◆ libusb_dev_mem_free()

int libusb_dev_mem_free | ( | libusb_device_handle * | dev_handle,
|  | unsigned char * | buffer,
|  | size_t | length
| ) |  |

Free device memory allocated with libusb_dev_mem_alloc().

Parameters

  dev_handle | a device handle
  buffer | pointer to the previously allocated memory
  length | size of previously allocated memory

Returns
  LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure

## ◆ libusb_alloc_transfer()

struct libusb_transfer * libusb_alloc_transfer | ( | int | iso_packets | ) |

Allocate a libusb transfer with a specified number of isochronous packet descriptors.

The returned transfer is pre-initialized for you. When the new transfer is no longer needed, it should be freed with libusb_free_transfer().

Transfers intended for non-isochronous endpoints (e.g. control, bulk, interrupt) should specify an iso_packets count of zero.

For transfers intended for isochronous endpoints, specify an appropriate number of packet descriptors to be allocated as part of the transfer. The returned transfer is not specially initialized for isochronous I/O; you are still required to set the num_iso_packets and type fields accordingly.

It is safe to allocate a transfer with some isochronous packets and then use it on a non-isochronous endpoint. If you do this, ensure that at time of submission, num_iso_packets is 0 and that type is set appropriately.

Parameters

  iso_packets | number of isochronous packet descriptors to allocate. Must be non-negative.

Returns
  a newly allocated transfer, or NULL on error

## ◆ libusb_free_transfer()

void libusb_free_transfer | ( | struct libusb_transfer * | transfer | ) |

Free a transfer structure.

This should be called for all transfers allocated with libusb_alloc_transfer().

If the LIBUSB_TRANSFER_FREE_BUFFER flag is set and the transfer buffer is non-NULL, this function will also free the transfer buffer using the standard system memory allocator (e.g. free()).

It is legal to call this function with a NULL transfer. In this case, the function will simply return safely.

It is not legal to free an active transfer (one which has been submitted and has not yet completed).

Parameters

  transfer | the transfer to free

## ◆ libusb_submit_transfer()

int libusb_submit_transfer | ( | struct libusb_transfer * | transfer | ) |

Submit a transfer.

This function will fire off the USB transfer and then return immediately.

Parameters

  transfer | the transfer to submit

Returns
  0 on success
  LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  LIBUSB_ERROR_BUSY if the transfer has already been submitted.
  LIBUSB_ERROR_NOT_SUPPORTED if the transfer flags are not supported by the operating system.
  LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than the operating system and/or hardware can support (see Transfer length limitations)
  another LIBUSB_ERROR code on other failure

## ◆ libusb_cancel_transfer()

int libusb_cancel_transfer | ( | struct libusb_transfer * | transfer | ) |

Asynchronously cancel a previously submitted transfer.

This function returns immediately, but this does not indicate cancellation is complete. Your callback function will be invoked at some later time with a transfer status of LIBUSB_TRANSFER_CANCELLED.

Parameters

  transfer | the transfer to cancel

Returns
  0 on success
  LIBUSB_ERROR_NOT_FOUND if the transfer is not in progress, already complete, or already cancelled.
  a LIBUSB_ERROR code on failure

## ◆ libusb_transfer_set_stream_id()

void libusb_transfer_set_stream_id | ( | struct libusb_transfer * | transfer,
|  | uint32_t | stream_id
| ) |  |

Set a transfers bulk stream id.

Note users are advised to use libusb_fill_bulk_stream_transfer() instead of calling this function directly.

Since version 1.0.19, LIBUSB_API_VERSION >= 0x01000103

Parameters

  transfer | the transfer to set the stream id for
  stream_id | the stream id to set

See also
  libusb_alloc_streams()

## ◆ libusb_transfer_get_stream_id()

uint32_t libusb_transfer_get_stream_id | ( | struct libusb_transfer * | transfer | ) |

Get a transfers bulk stream id.

Since version 1.0.19, LIBUSB_API_VERSION >= 0x01000103

Parameters

  transfer | the transfer to get the stream id for

Returns
  the stream id for the transfer

## ◆ libusb_control_transfer_get_data()

static unsigned char * libusb_control_transfer_get_data | ( | struct libusb_transfer * | transfer | ) |
| inline static

Get the data section of a control transfer.

This convenience function is here to remind you that the data does not start until 8 bytes into the actual buffer, as the setup packet comes first.

Calling this function only makes sense from a transfer callback function, or situations where you have already allocated a suitably sized buffer at transfer->buffer.

Parameters

  transfer | a transfer

Returns
  pointer to the first byte of the data section

## ◆ libusb_control_transfer_get_setup()

static struct libusb_control_setup * libusb_control_transfer_get_setup | ( | struct libusb_transfer * | transfer | ) |
| inline static

Get the control setup packet of a control transfer.

This convenience function is here to remind you that the control setup occupies the first 8 bytes of the transfer data buffer.

Calling this function only makes sense from a transfer callback function, or situations where you have already allocated a suitably sized buffer at transfer->buffer.

Parameters

  transfer | a transfer

Returns
  a casted pointer to the start of the transfer data buffer

## ◆ libusb_fill_control_setup()

static void libusb_fill_control_setup | ( | unsigned char * | buffer,
|  | uint8_t | bmRequestType,
|  | uint8_t | bRequest,
|  | uint16_t | wValue,
|  | uint16_t | wIndex,
|  | uint16_t | wLength
| ) |  |
| inline static

Helper function to populate the setup packet (first 8 bytes of the data buffer) for a control transfer.

The wIndex, wValue and wLength values should be given in host-endian byte order.

Parameters

  buffer | buffer to output the setup packet into This pointer must be aligned to at least 2 bytes boundary.
  bmRequestType | see the bmRequestType field of libusb_control_setup
  bRequest | see the bRequest field of libusb_control_setup
  wValue | see the wValue field of libusb_control_setup
  wIndex | see the wIndex field of libusb_control_setup
  wLength | see the wLength field of libusb_control_setup

## ◆ libusb_fill_control_transfer()

static void libusb_fill_control_transfer | ( | struct libusb_transfer * | transfer,
|  | libusb_device_handle * | dev_handle,
|  | unsigned char * | buffer,
|  | libusb_transfer_cb_fn | callback,
|  | void * | user_data,
|  | unsigned int | timeout
| ) |  |
| inline static

Helper function to populate the required libusb_transfer fields for a control transfer.

If you pass a transfer buffer to this function, the first 8 bytes will be interpreted as a control setup packet, and the wLength field will be used to automatically populate the length field of the transfer. Therefore the recommended approach is:

1. Allocate a suitably sized data buffer (including space for control setup)
2. Call libusb_fill_control_setup()
3. If this is a host-to-device transfer with a data stage, put the data in place after the setup packet
4. Call this function
5. Call libusb_submit_transfer()

It is also legal to pass a NULL buffer to this function, in which case this function will not attempt to populate the length field. Remember that you must then populate the buffer and length fields later.

Parameters

  transfer | the transfer to populate
  dev_handle | handle of the device that will handle the transfer
  buffer | data buffer. If provided, this function will interpret the first 8 bytes as a setup packet and infer the transfer length from that. This pointer must be aligned to at least 2 bytes boundary.
  callback | callback function to be invoked on transfer completion
  user_data | user data to pass to callback function
  timeout | timeout for the transfer in milliseconds

## ◆ libusb_fill_bulk_transfer()

static void libusb_fill_bulk_transfer | ( | struct libusb_transfer * | transfer,
|  | libusb_device_handle * | dev_handle,
|  | unsigned char | endpoint,
|  | unsigned char * | buffer,
|  | int | length,
|  | libusb_transfer_cb_fn | callback,
|  | void * | user_data,
|  | unsigned int | timeout
| ) |  |
| inline static

Helper function to populate the required libusb_transfer fields for a bulk transfer.

Parameters

  transfer | the transfer to populate
  dev_handle | handle of the device that will handle the transfer
  endpoint | address of the endpoint where this transfer will be sent
  buffer | data buffer
  length | length of data buffer
  callback | callback function to be invoked on transfer completion
  user_data | user data to pass to callback function
  timeout | timeout for the transfer in milliseconds

## ◆ libusb_fill_bulk_stream_transfer()

static void libusb_fill_bulk_stream_transfer | ( | struct libusb_transfer * | transfer,
|  | libusb_device_handle * | dev_handle,
|  | unsigned char | endpoint,
|  | uint32_t | stream_id,
|  | unsigned char * | buffer,
|  | int | length,
|  | libusb_transfer_cb_fn | callback,
|  | void * | user_data,
|  | unsigned int | timeout
| ) |  |
| inline static

Helper function to populate the required libusb_transfer fields for a bulk transfer using bulk streams.

Since version 1.0.19, LIBUSB_API_VERSION >= 0x01000103

Parameters

  transfer | the transfer to populate
  dev_handle | handle of the device that will handle the transfer
  endpoint | address of the endpoint where this transfer will be sent
  stream_id | bulk stream id for this transfer
  buffer | data buffer
  length | length of data buffer
  callback | callback function to be invoked on transfer completion
  user_data | user data to pass to callback function
  timeout | timeout for the transfer in milliseconds

## ◆ libusb_fill_interrupt_transfer()

static void libusb_fill_interrupt_transfer | ( | struct libusb_transfer * | transfer,
|  | libusb_device_handle * | dev_handle,
|  | unsigned char | endpoint,
|  | unsigned char * | buffer,
|  | int | length,
|  | libusb_transfer_cb_fn | callback,
|  | void * | user_data,
|  | unsigned int | timeout
| ) |  |
| inline static

Helper function to populate the required libusb_transfer fields for an interrupt transfer.

Parameters

  transfer | the transfer to populate
  dev_handle | handle of the device that will handle the transfer
  endpoint | address of the endpoint where this transfer will be sent
  buffer | data buffer
  length | length of data buffer
  callback | callback function to be invoked on transfer completion
  user_data | user data to pass to callback function
  timeout | timeout for the transfer in milliseconds

## ◆ libusb_fill_iso_transfer()

static void libusb_fill_iso_transfer | ( | struct libusb_transfer * | transfer,
|  | libusb_device_handle * | dev_handle,
|  | unsigned char | endpoint,
|  | unsigned char * | buffer,
|  | int | length,
|  | int | num_iso_packets,
|  | libusb_transfer_cb_fn | callback,
|  | void * | user_data,
|  | unsigned int | timeout
| ) |  |
| inline static

Helper function to populate the required libusb_transfer fields for an isochronous transfer.

Parameters

  transfer | the transfer to populate
  dev_handle | handle of the device that will handle the transfer
  endpoint | address of the endpoint where this transfer will be sent
  buffer | data buffer
  length | length of data buffer
  num_iso_packets | the number of isochronous packets
  callback | callback function to be invoked on transfer completion
  user_data | user data to pass to callback function
  timeout | timeout for the transfer in milliseconds

## ◆ libusb_set_iso_packet_lengths()

static void libusb_set_iso_packet_lengths | ( | struct libusb_transfer * | transfer,
|  | unsigned int | length
| ) |  |
| inline static

Convenience function to set the length of all packets in an isochronous transfer, based on the num_iso_packets field in the transfer structure.

Parameters

  transfer | a transfer
  length | the length to set in each isochronous packet descriptor

See also
  libusb_get_max_packet_size()

## ◆ libusb_get_iso_packet_buffer()

static unsigned char * libusb_get_iso_packet_buffer | ( | struct libusb_transfer * | transfer,
|  | unsigned int | packet
| ) |  |
| inline static

Convenience function to locate the position of an isochronous packet within the buffer of an isochronous transfer.

This is a thorough function which loops through all preceding packets, accumulating their lengths to find the position of the specified packet. Typically you will assign equal lengths to each packet in the transfer, and hence the above method is sub-optimal. You may wish to use libusb_get_iso_packet_buffer_simple() instead.

Parameters

  transfer | a transfer
  packet | the packet to return the address of

Returns
  the base address of the packet buffer inside the transfer buffer, or NULL if the packet does not exist.

See also
  libusb_get_iso_packet_buffer_simple()

## ◆ libusb_get_iso_packet_buffer_simple()

static unsigned char * libusb_get_iso_packet_buffer_simple | ( | struct libusb_transfer * | transfer,
|  | unsigned int | packet
| ) |  |
| inline static

Convenience function to locate the position of an isochronous packet within the buffer of an isochronous transfer, for transfers where each packet is of identical size.

This function relies on the assumption that every packet within the transfer is of identical size to the first packet. Calculating the location of the packet buffer is then just a simple calculation: `buffer + (packet_size * packet)`

Do not use this function on transfers other than those that have identical packet lengths for each packet.

Parameters

  transfer | a transfer
  packet | the packet to return the address of

Returns
  the base address of the packet buffer inside the transfer buffer, or NULL if the packet does not exist.

See also
  libusb_get_iso_packet_buffer()