libusb
1.0.27
A cross-platform user library to access USB devices
|
The functionality documented below is designed to help with the following operations: More...
Typedefs | |
typedef struct libusb_device | libusb_device |
Structure representing a USB device detected on the system. More... | |
typedef struct libusb_device_handle | libusb_device_handle |
Structure representing a handle on a USB device. More... | |
Enumerations | |
enum | libusb_speed { LIBUSB_SPEED_UNKNOWN = 0 , LIBUSB_SPEED_LOW = 1 , LIBUSB_SPEED_FULL = 2 , LIBUSB_SPEED_HIGH = 3 , LIBUSB_SPEED_SUPER = 4 , LIBUSB_SPEED_SUPER_PLUS = 5 } |
Speed codes. More... | |
Functions | |
ssize_t | libusb_get_device_list (libusb_context *ctx, libusb_device ***list) |
Returns a list of USB devices currently attached to the system. More... | |
void | libusb_free_device_list (libusb_device **list, int unref_devices) |
Frees a list of devices previously discovered using libusb_get_device_list(). More... | |
uint8_t | libusb_get_bus_number (libusb_device *dev) |
Get the number of the bus that a device is connected to. More... | |
uint8_t | libusb_get_port_number (libusb_device *dev) |
Get the number of the port that a device is connected to. More... | |
int | libusb_get_port_numbers (libusb_device *dev, uint8_t *port_numbers, int port_numbers_len) |
Get the list of all port numbers from root for the specified device. More... | |
int | libusb_get_port_path (libusb_context *ctx, libusb_device *dev, uint8_t *port_numbers, uint8_t port_numbers_len) |
libusb_device * | libusb_get_parent (libusb_device *dev) |
Get the the parent from the specified device. More... | |
uint8_t | libusb_get_device_address (libusb_device *dev) |
Get the address of the device on the bus it is connected to. More... | |
int | libusb_get_device_speed (libusb_device *dev) |
Get the negotiated connection speed for a device. More... | |
int | libusb_get_max_packet_size (libusb_device *dev, unsigned char endpoint) |
Convenience function to retrieve the wMaxPacketSize value for a particular endpoint in the active device configuration. More... | |
int | libusb_get_max_iso_packet_size (libusb_device *dev, unsigned char endpoint) |
Calculate the maximum packet size which a specific endpoint is capable is sending or receiving in the duration of 1 microframe. More... | |
int | libusb_get_max_alt_packet_size (libusb_device *dev, int interface_number, int alternate_setting, unsigned char endpoint) |
Calculate the maximum packet size which a specific endpoint is capable of sending or receiving in the duration of 1 microframe. More... | |
libusb_device * | libusb_ref_device (libusb_device *dev) |
Increment the reference count of a device. More... | |
void | libusb_unref_device (libusb_device *dev) |
Decrement the reference count of a device. More... | |
int | libusb_wrap_sys_device (libusb_context *ctx, intptr_t sys_dev, libusb_device_handle **dev_handle) |
Wrap a platform-specific system device handle and obtain a libusb device handle for the underlying device. More... | |
int | libusb_open (libusb_device *dev, libusb_device_handle **dev_handle) |
Open a device and obtain a device handle. More... | |
libusb_device_handle * | libusb_open_device_with_vid_pid (libusb_context *ctx, uint16_t vendor_id, uint16_t product_id) |
Convenience function for finding a device with a particular idVendor /idProduct combination. More... | |
void | libusb_close (libusb_device_handle *dev_handle) |
Close a device handle. More... | |
libusb_device * | libusb_get_device (libusb_device_handle *dev_handle) |
Get the underlying device for a device handle. More... | |
int | libusb_get_configuration (libusb_device_handle *dev_handle, int *config) |
Determine the bConfigurationValue of the currently active configuration. More... | |
int | libusb_set_configuration (libusb_device_handle *dev_handle, int configuration) |
Set the active configuration for a device. More... | |
int | libusb_claim_interface (libusb_device_handle *dev_handle, int interface_number) |
Claim an interface on a given device handle. More... | |
int | libusb_release_interface (libusb_device_handle *dev_handle, int interface_number) |
Release an interface previously claimed with libusb_claim_interface(). More... | |
int | libusb_set_interface_alt_setting (libusb_device_handle *dev_handle, int interface_number, int alternate_setting) |
Activate an alternate setting for an interface. More... | |
int | libusb_clear_halt (libusb_device_handle *dev_handle, unsigned char endpoint) |
Clear the halt/stall condition for an endpoint. More... | |
int | libusb_reset_device (libusb_device_handle *dev_handle) |
Perform a USB port reset to reinitialize a device. More... | |
int | libusb_kernel_driver_active (libusb_device_handle *dev_handle, int interface_number) |
Determine if a kernel driver is active on an interface. More... | |
int | libusb_detach_kernel_driver (libusb_device_handle *dev_handle, int interface_number) |
Detach a kernel driver from an interface. More... | |
int | libusb_attach_kernel_driver (libusb_device_handle *dev_handle, int interface_number) |
Re-attach an interface's kernel driver, which was previously detached using libusb_detach_kernel_driver(). More... | |
int | libusb_set_auto_detach_kernel_driver (libusb_device_handle *dev_handle, int enable) |
Enable/disable libusb's automatic kernel driver detachment. More... | |
The functionality documented below is designed to help with the following operations:
The description below really makes things sound more complicated than they actually are. The following sequence of function calls will be suitable for almost all scenarios and does not require you to have such a deep understanding of the resource management issues:
The two important points:
If you ended up with a handle, you can now proceed to perform I/O on the device.
libusb has a concept of a USB device, represented by the libusb_device opaque type. A device represents a USB device that is currently or was previously connected to the system. Using a reference to a device, you can determine certain information about the device (e.g. you can read the descriptor data).
The libusb_get_device_list() function can be used to obtain a list of devices currently connected to the system. This is known as device discovery. Devices can also be discovered with the hotplug mechanism, whereby a callback function registered with libusb_hotplug_register_callback() will be called when a device of interest is connected or disconnected.
Just because you have a reference to a device does not mean it is necessarily usable. The device may have been unplugged, you may not have permission to operate such device, or another program or driver may be using the device.
When you've found a device that you'd like to operate, you must ask libusb to open the device using the libusb_open() function. Assuming success, libusb then returns you a device handle (a libusb_device_handle pointer). All "real" I/O operations then operate on the handle rather than the original device pointer.
Device discovery (i.e. calling libusb_get_device_list()) returns a freshly-allocated list of devices. The list itself must be freed when you are done with it. libusb also needs to know when it is OK to free the contents of the list - the devices themselves.
To handle these issues, libusb provides you with two separate items:
New devices presented by the libusb_get_device_list() function all have a reference count of 1. You can increase and decrease reference count using libusb_ref_device() and libusb_unref_device(). A device is destroyed when its reference count reaches 0.
With the above information in mind, the process of opening a device can be viewed as follows:
The order is important - you must not unreference the device before attempting to open it, because unreferencing it may destroy the device.
For convenience, the libusb_free_device_list() function includes a parameter to optionally unreference all the devices in the list before freeing the list itself. This combines steps 3 and 4 above.
As an implementation detail, libusb_open() actually adds a reference to the device in question. This is because the device remains available through the handle via libusb_get_device(). The reference is deleted during libusb_close().
typedef struct libusb_device libusb_device |
Structure representing a USB device detected on the system.
This is an opaque type for which you are only ever provided with a pointer, usually originating from libusb_get_device_list() or libusb_hotplug_register_callback().
Certain operations can be performed on a device, but in order to do any I/O you will have to first obtain a device handle using libusb_open().
Devices are reference counted with libusb_ref_device() and libusb_unref_device(), and are freed when the reference count reaches 0. New devices presented by libusb_get_device_list() have a reference count of 1, and libusb_free_device_list() can optionally decrease the reference count on all devices in the list. libusb_open() adds another reference which is later destroyed by libusb_close().
typedef struct libusb_device_handle libusb_device_handle |
Structure representing a handle on a USB device.
This is an opaque type for which you are only ever provided with a pointer, usually originating from libusb_open().
A device handle is used to perform I/O and other operations. When finished with a device handle, you should call libusb_close().
enum libusb_speed |
Speed codes.
Indicates the speed at which the device is operating.
ssize_t libusb_get_device_list | ( | libusb_context * | ctx, |
libusb_device *** | list | ||
) |
Returns a list of USB devices currently attached to the system.
This is your entry point into finding a USB device to operate.
You are expected to unreference all the devices when you are done with them, and then free the list with libusb_free_device_list(). Note that libusb_free_device_list() can unref all the devices for you. Be careful not to unreference a device you are about to open until after you have opened it.
This return value of this function indicates the number of devices in the resultant list. The list is actually one element larger, as it is NULL-terminated.
ctx | the context to operate on, or NULL for the default context |
list | output location for a list of devices. Must be later freed with libusb_free_device_list(). |
void libusb_free_device_list | ( | libusb_device ** | list, |
int | unref_devices | ||
) |
Frees a list of devices previously discovered using libusb_get_device_list().
If the unref_devices parameter is set, the reference count of each device in the list is decremented by 1.
list | the list to free |
unref_devices | whether to unref the devices in the list |
uint8_t libusb_get_bus_number | ( | libusb_device * | dev | ) |
Get the number of the bus that a device is connected to.
dev | a device |
uint8_t libusb_get_port_number | ( | libusb_device * | dev | ) |
Get the number of the port that a device is connected to.
Unless the OS does something funky, or you are hot-plugging USB extension cards, the port number returned by this call is usually guaranteed to be uniquely tied to a physical port, meaning that different devices plugged on the same physical port should return the same port number.
But outside of this, there is no guarantee that the port number returned by this call will remain the same, or even match the order in which ports have been numbered by the HUB/HCD manufacturer.
dev | a device |
int libusb_get_port_numbers | ( | libusb_device * | dev, |
uint8_t * | port_numbers, | ||
int | port_numbers_len | ||
) |
Get the list of all port numbers from root for the specified device.
Since version 1.0.16, LIBUSBX_API_VERSION >= 0x01000102
dev | a device |
port_numbers | the array that should contain the port numbers |
port_numbers_len | the maximum length of the array. As per the USB 3.0 specs, the current maximum limit for the depth is 7. |
int libusb_get_port_path | ( | libusb_context * | ctx, |
libusb_device * | dev, | ||
uint8_t * | port_numbers, | ||
uint8_t | port_numbers_len | ||
) |
libusb_device* libusb_get_parent | ( | libusb_device * | dev | ) |
Get the the parent from the specified device.
dev | a device |
uint8_t libusb_get_device_address | ( | libusb_device * | dev | ) |
Get the address of the device on the bus it is connected to.
dev | a device |
int libusb_get_device_speed | ( | libusb_device * | dev | ) |
Get the negotiated connection speed for a device.
dev | a device |
int libusb_get_max_packet_size | ( | libusb_device * | dev, |
unsigned char | endpoint | ||
) |
Convenience function to retrieve the wMaxPacketSize value for a particular endpoint in the active device configuration.
This function was originally intended to be of assistance when setting up isochronous transfers, but a design mistake resulted in this function instead. It simply returns the wMaxPacketSize value without considering its contents. If you're dealing with isochronous transfers, you probably want libusb_get_max_iso_packet_size() instead.
dev | a device |
endpoint | address of the endpoint in question |
int libusb_get_max_iso_packet_size | ( | libusb_device * | dev, |
unsigned char | endpoint | ||
) |
Calculate the maximum packet size which a specific endpoint is capable is sending or receiving in the duration of 1 microframe.
Only the active configuration is examined. The calculation is based on the wMaxPacketSize field in the endpoint descriptor as described in section 9.6.6 in the USB 2.0 specifications.
If acting on an isochronous or interrupt endpoint, this function will multiply the value found in bits 0:10 by the number of transactions per microframe (determined by bits 11:12). Otherwise, this function just returns the numeric value found in bits 0:10. For USB 3.0 device, it will attempts to retrieve the Endpoint Companion Descriptor to return wBytesPerInterval.
This function is useful for setting up isochronous transfers, for example you might pass the return value from this function to libusb_set_iso_packet_lengths() in order to set the length field of every isochronous packet in a transfer.
This function only considers the first alternate setting of the interface. If the endpoint has different maximum packet sizes for different alternate settings, you probably want libusb_get_max_alt_packet_size() instead.
Since v1.0.3.
dev | a device |
endpoint | address of the endpoint in question |
int libusb_get_max_alt_packet_size | ( | libusb_device * | dev, |
int | interface_number, | ||
int | alternate_setting, | ||
unsigned char | endpoint | ||
) |
Calculate the maximum packet size which a specific endpoint is capable of sending or receiving in the duration of 1 microframe.
Only the active configuration is examined. The calculation is based on the wMaxPacketSize field in the endpoint descriptor as described in section 9.6.6 in the USB 2.0 specifications.
If acting on an isochronous or interrupt endpoint, this function will multiply the value found in bits 0:10 by the number of transactions per microframe (determined by bits 11:12). Otherwise, this function just returns the numeric value found in bits 0:10. For USB 3.0 device, it will attempts to retrieve the Endpoint Companion Descriptor to return wBytesPerInterval.
This function is useful for setting up isochronous transfers, for example you might pass the return value from this function to libusb_set_iso_packet_lengths() in order to set the length field of every isochronous packet in a transfer.
Since version 1.0.27, LIBUSB_API_VERSION >= 0x0100010A
dev | a device |
interface_number | the bInterfaceNumber of the interface the endpoint belongs to |
alternate_setting | the bAlternateSetting of the interface |
endpoint | address of the endpoint in question |
libusb_device* libusb_ref_device | ( | libusb_device * | dev | ) |
Increment the reference count of a device.
dev | the device to reference |
void libusb_unref_device | ( | libusb_device * | dev | ) |
Decrement the reference count of a device.
If the decrement operation causes the reference count to reach zero, the device shall be destroyed.
dev | the device to unreference |
int libusb_wrap_sys_device | ( | libusb_context * | ctx, |
intptr_t | sys_dev, | ||
libusb_device_handle ** | dev_handle | ||
) |
Wrap a platform-specific system device handle and obtain a libusb device handle for the underlying device.
The handle allows you to use libusb to perform I/O on the device in question.
Call libusb_init_context with the LIBUSB_OPTION_NO_DEVICE_DISCOVERY option if you want to skip enumeration of USB devices. In particular, this might be needed on Android if you don't have authority to access USB devices in general. Setting this option with libusb_set_option is deprecated.
On Linux, the system device handle must be a valid file descriptor opened on the device node.
The system device handle must remain open until libusb_close() is called. The system device handle will not be closed by libusb_close().
Internally, this function creates a temporary device and makes it available to you through libusb_get_device(). This device is destroyed during libusb_close(). The device shall not be opened through libusb_open().
This is a non-blocking function; no requests are sent over the bus.
Since version 1.0.23, LIBUSB_API_VERSION >= 0x01000107
ctx | the context to operate on, or NULL for the default context |
sys_dev | the platform-specific system device handle |
dev_handle | output location for the returned device handle pointer. Only populated when the return code is 0. |
int libusb_open | ( | libusb_device * | dev, |
libusb_device_handle ** | dev_handle | ||
) |
Open a device and obtain a device handle.
A handle allows you to perform I/O on the device in question.
Internally, this function adds a reference to the device and makes it available to you through libusb_get_device(). This reference is removed during libusb_close().
This is a non-blocking function; no requests are sent over the bus.
dev | the device to open |
dev_handle | output location for the returned device handle pointer. Only populated when the return code is 0. |
libusb_device_handle* libusb_open_device_with_vid_pid | ( | libusb_context * | ctx, |
uint16_t | vendor_id, | ||
uint16_t | product_id | ||
) |
Convenience function for finding a device with a particular idVendor
/idProduct
combination.
This function is intended for those scenarios where you are using libusb to knock up a quick test application - it allows you to avoid calling libusb_get_device_list() and worrying about traversing/freeing the list.
This function has limitations and is hence not intended for use in real applications: if multiple devices have the same IDs it will only give you the first one, etc.
ctx | the context to operate on, or NULL for the default context |
vendor_id | the idVendor value to search for |
product_id | the idProduct value to search for |
void libusb_close | ( | libusb_device_handle * | dev_handle | ) |
Close a device handle.
Should be called on all open handles before your application exits.
Internally, this function destroys the reference that was added by libusb_open() on the given device.
This is a non-blocking function; no requests are sent over the bus.
dev_handle | the device handle to close |
libusb_device* libusb_get_device | ( | libusb_device_handle * | dev_handle | ) |
Get the underlying device for a device handle.
This function does not modify the reference count of the returned device, so do not feel compelled to unreference it when you are done.
dev_handle | a device handle |
int libusb_get_configuration | ( | libusb_device_handle * | dev_handle, |
int * | config | ||
) |
Determine the bConfigurationValue of the currently active configuration.
You could formulate your own control request to obtain this information, but this function has the advantage that it may be able to retrieve the information from operating system caches (no I/O involved).
If the OS does not cache this information, then this function will block while a control transfer is submitted to retrieve the information.
This function will return a value of 0 in the config
output parameter if the device is in unconfigured state.
dev_handle | a device handle |
config | output location for the bConfigurationValue of the active configuration (only valid for return code 0) |
int libusb_set_configuration | ( | libusb_device_handle * | dev_handle, |
int | configuration | ||
) |
Set the active configuration for a device.
The operating system may or may not have already set an active configuration on the device. It is up to your application to ensure the correct configuration is selected before you attempt to claim interfaces and perform other operations.
If you call this function on a device already configured with the selected configuration, then this function will act as a lightweight device reset: it will issue a SET_CONFIGURATION request using the current configuration, causing most USB-related device state to be reset (altsetting reset to zero, endpoint halts cleared, toggles reset).
Not all backends support setting the configuration from user space, which will be indicated by the return code LIBUSB_ERROR_NOT_SUPPORTED. As this suggests that the platform is handling the device configuration itself, this error should generally be safe to ignore.
You cannot change/reset configuration if your application has claimed interfaces. It is advised to set the desired configuration before claiming interfaces.
Alternatively you can call libusb_release_interface() first. Note if you do things this way you must ensure that auto_detach_kernel_driver for dev
is 0, otherwise the kernel driver will be re-attached when you release the interface(s).
You cannot change/reset configuration if other applications or drivers have claimed interfaces.
A configuration value of -1 will put the device in unconfigured state. The USB specifications state that a configuration value of 0 does this, however buggy devices exist which actually have a configuration 0.
You should always use this function rather than formulating your own SET_CONFIGURATION control request. This is because the underlying operating system needs to know when such changes happen.
This is a blocking function.
dev_handle | a device handle |
configuration | the bConfigurationValue of the configuration you wish to activate, or -1 if you wish to put the device in an unconfigured state |
int libusb_claim_interface | ( | libusb_device_handle * | dev_handle, |
int | interface_number | ||
) |
Claim an interface on a given device handle.
You must claim the interface you wish to use before you can perform I/O on any of its endpoints.
It is legal to attempt to claim an already-claimed interface, in which case libusb just returns 0 without doing anything.
If auto_detach_kernel_driver is set to 1 for dev
, the kernel driver will be detached if necessary, on failure the detach error is returned.
Claiming of interfaces is a purely logical operation; it does not cause any requests to be sent over the bus. Interface claiming is used to instruct the underlying operating system that your application wishes to take ownership of the interface.
This is a non-blocking function.
dev_handle | a device handle |
interface_number | the bInterfaceNumber of the interface you wish to claim |
int libusb_release_interface | ( | libusb_device_handle * | dev_handle, |
int | interface_number | ||
) |
Release an interface previously claimed with libusb_claim_interface().
You should release all claimed interfaces before closing a device handle.
This is a blocking function. A SET_INTERFACE control request will be sent to the device, resetting interface state to the first alternate setting.
If auto_detach_kernel_driver is set to 1 for dev
, the kernel driver will be re-attached after releasing the interface.
dev_handle | a device handle |
interface_number | the bInterfaceNumber of the previously-claimed interface |
int libusb_set_interface_alt_setting | ( | libusb_device_handle * | dev_handle, |
int | interface_number, | ||
int | alternate_setting | ||
) |
Activate an alternate setting for an interface.
The interface must have been previously claimed with libusb_claim_interface().
You should always use this function rather than formulating your own SET_INTERFACE control request. This is because the underlying operating system needs to know when such changes happen.
This is a blocking function.
dev_handle | a device handle |
interface_number | the bInterfaceNumber of the previously-claimed interface |
alternate_setting | the bAlternateSetting of the alternate setting to activate |
int libusb_clear_halt | ( | libusb_device_handle * | dev_handle, |
unsigned char | endpoint | ||
) |
Clear the halt/stall condition for an endpoint.
Endpoints with halt status are unable to receive or transmit data until the halt condition is stalled.
You should cancel all pending transfers before attempting to clear the halt condition.
This is a blocking function.
dev_handle | a device handle |
endpoint | the endpoint to clear halt status |
int libusb_reset_device | ( | libusb_device_handle * | dev_handle | ) |
Perform a USB port reset to reinitialize a device.
The system will attempt to restore the previous configuration and alternate settings after the reset has completed.
If the reset fails, the descriptors change, or the previous state cannot be restored, the device will appear to be disconnected and reconnected. This means that the device handle is no longer valid (you should close it) and rediscover the device. A return code of LIBUSB_ERROR_NOT_FOUND indicates when this is the case.
This is a blocking function which usually incurs a noticeable delay.
dev_handle | a handle of the device to reset |
int libusb_kernel_driver_active | ( | libusb_device_handle * | dev_handle, |
int | interface_number | ||
) |
Determine if a kernel driver is active on an interface.
If a kernel driver is active, you cannot claim the interface, and libusb will be unable to perform I/O.
This functionality is not available on Windows.
dev_handle | a device handle |
interface_number | the interface to check |
int libusb_detach_kernel_driver | ( | libusb_device_handle * | dev_handle, |
int | interface_number | ||
) |
Detach a kernel driver from an interface.
If successful, you will then be able to claim the interface and perform I/O.
This functionality is not available on Windows.
Note that libusb itself also talks to the device through a special kernel driver, if this driver is already attached to the device, this call will not detach it and return LIBUSB_ERROR_NOT_FOUND.
dev_handle | a device handle |
interface_number | the interface to detach the driver from |
int libusb_attach_kernel_driver | ( | libusb_device_handle * | dev_handle, |
int | interface_number | ||
) |
Re-attach an interface's kernel driver, which was previously detached using libusb_detach_kernel_driver().
This functionality is not available on Windows.
dev_handle | a device handle |
interface_number | the interface to attach the driver from |
int libusb_set_auto_detach_kernel_driver | ( | libusb_device_handle * | dev_handle, |
int | enable | ||
) |
Enable/disable libusb's automatic kernel driver detachment.
When this is enabled libusb will automatically detach the kernel driver on an interface when claiming the interface, and attach it when releasing the interface.
Automatic kernel driver detachment is disabled on newly opened device handles by default.
On platforms which do not have LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER this function will return LIBUSB_ERROR_NOT_SUPPORTED, and libusb will continue as if this function was never called.
dev_handle | a device handle |
enable | whether to enable or disable auto kernel driver detachment |