With the above information in mind, the process of opening a device can be viewed as follows:
-
Discover devices using
libusb_get_device_list()
or
libusb_hotplug_register_callback()
.
-
Choose the device that you want to operate, and call
libusb_open()
.
-
Unref all devices in the discovered device list.
-
Free the discovered device list.
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()
.
◆
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()
.
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()
.
◆
libusb_speed
Indicates the speed at which the device is operating.
Enumerator
LIBUSB_SPEED_UNKNOWN
The OS doesn't report or know the device speed.
LIBUSB_SPEED_LOW
The device is operating at low speed (1.5MBit/s).
LIBUSB_SPEED_FULL
The device is operating at full speed (12MBit/s).
LIBUSB_SPEED_HIGH
The device is operating at high speed (480MBit/s).
LIBUSB_SPEED_SUPER
The device is operating at super speed (5000MBit/s).
LIBUSB_SPEED_SUPER_PLUS
The device is operating at super speed plus (10000MBit/s).
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.
-
Parameters
-
ctxthe context to operate on, or NULL for the default context
listoutput location for a list of devices. Must be later freed with
libusb_free_device_list()
.
-
Returns
-
the number of devices in the outputted list, or any
libusb_error
according to errors encountered by the backend.
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.
-
Parameters
-
listthe list to free
unref_deviceswhether to unref the devices in the list
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.
-
Parameters
-
deva device
-
Returns
-
the port number (0 if not available)
Get the list of all port numbers from root for the specified device.
Since version 1.0.16,
LIBUSB_API_VERSION
>= 0x01000102
-
Parameters
-
deva device
port_numbersthe array that should contain the port numbers
port_numbers_lenthe maximum length of the array. As per the USB 3.0 specs, the current maximum limit for the depth is 7.
-
Returns
-
the number of elements filled
LIBUSB_ERROR_OVERFLOW
if the array is too small
-
Returns
-
the device parent or NULL if not available You should issue a
libusb_get_device_list()
before calling this function and make sure that you only access the parent before issuing
libusb_free_device_list()
. The reason is that libusb currently does not maintain a permanent list of device instances, and therefore can only guarantee that parents are fully instantiated within a
libusb_get_device_list()
-
libusb_free_device_list()
block.
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.
-
Parameters
-
deva device
endpointaddress of the endpoint in question
-
Returns
-
the wMaxPacketSize value
LIBUSB_ERROR_NOT_FOUND
if the endpoint does not exist
LIBUSB_ERROR_OTHER
on other failure
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.
Since v1.0.3.
-
Parameters
-
deva device
endpointaddress of the endpoint in question
-
Returns
-
the maximum packet size which can be sent/received on this endpoint
LIBUSB_ERROR_NOT_FOUND
if the endpoint does not exist
LIBUSB_ERROR_OTHER
on other failure
Decrement the reference count of a device.
If the decrement operation causes the reference count to reach zero, the device shall be destroyed.
-
Parameters
-
devthe device to unreference
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_set_option()
with the parameters (NULL,
LIBUSB_OPTION_NO_DEVICE_DISCOVERY
) before
libusb_init()
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.
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
-
Parameters
-
ctxthe context to operate on, or NULL for the default context
sys_devthe platform-specific system device handle
dev_handleoutput location for the returned device handle pointer. Only populated when the return code is 0.
-
Returns
-
0 on success
LIBUSB_ERROR_NO_MEM
on memory allocation failure
LIBUSB_ERROR_ACCESS
if the user has insufficient permissions
LIBUSB_ERROR_NOT_SUPPORTED
if the operation is not supported on this platform
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
devthe device to open
dev_handleoutput location for the returned device handle pointer. Only populated when the return code is 0.
-
Returns
-
0 on success
LIBUSB_ERROR_NO_MEM
on memory allocation failure
LIBUSB_ERROR_ACCESS
if the user has insufficient permissions
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
ctxthe context to operate on, or NULL for the default context
vendor_idthe idVendor value to search for
product_idthe idProduct value to search for
-
Returns
-
a device handle for the first found device, or NULL on error or if the device could not be found.
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.
-
Parameters
-
dev_handlethe device handle to close
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.
-
Parameters
-
dev_handlea device handle
-
Returns
-
the underlying device
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.
-
Parameters
-
dev_handlea device handle
configoutput location for the bConfigurationValue of the active configuration (only valid for return code 0)
-
Returns
-
0 on success
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
dev_handlea device handle
configurationthe bConfigurationValue of the configuration you wish to activate, or -1 if you wish to put the device in an unconfigured state
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if the requested configuration does not exist
LIBUSB_ERROR_BUSY
if interfaces are currently claimed
LIBUSB_ERROR_NOT_SUPPORTED
if setting or changing the configuration is not supported by the backend
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
-
See also
-
libusb_set_auto_detach_kernel_driver()
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.
-
Parameters
-
dev_handlea device handle
interface_numberthe
bInterfaceNumber
of the interface you wish to claim
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if the requested interface does not exist
LIBUSB_ERROR_BUSY
if another program or driver has claimed the interface
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
a LIBUSB_ERROR code on other failure
-
See also
-
libusb_set_auto_detach_kernel_driver()
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.
-
Parameters
-
dev_handlea device handle
interface_numberthe
bInterfaceNumber
of the previously-claimed interface
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if the interface was not claimed
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
-
See also
-
libusb_set_auto_detach_kernel_driver()
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.
-
Parameters
-
dev_handlea device handle
interface_numberthe
bInterfaceNumber
of the previously-claimed interface
alternate_settingthe
bAlternateSetting
of the alternate setting to activate
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if the interface was not claimed, or the requested alternate setting does not exist
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
dev_handlea device handle
endpointthe endpoint to clear halt status
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if the endpoint does not exist
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
dev_handlea handle of the device to reset
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if re-enumeration is required, or if the device has been disconnected
another LIBUSB_ERROR code on other failure
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.
-
Parameters
-
dev_handlea device handle
interface_numberthe interface to check
-
Returns
-
0 if no kernel driver is active
1 if a kernel driver is active
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
LIBUSB_ERROR_NOT_SUPPORTED
on platforms where the functionality is not available
another LIBUSB_ERROR code on other failure
-
See also
-
libusb_detach_kernel_driver()
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
.
-
Parameters
-
dev_handlea device handle
interface_numberthe interface to detach the driver from
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if no kernel driver was active
LIBUSB_ERROR_INVALID_PARAM
if the interface does not exist
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
LIBUSB_ERROR_NOT_SUPPORTED
on platforms where the functionality is not available
another LIBUSB_ERROR code on other failure
-
See also
-
libusb_kernel_driver_active()
Re-attach an interface's kernel driver, which was previously detached using
libusb_detach_kernel_driver()
.
This functionality is not available on Windows.
-
Parameters
-
dev_handlea device handle
interface_numberthe interface to attach the driver from
-
Returns
-
0 on success
LIBUSB_ERROR_NOT_FOUND
if no kernel driver was active
LIBUSB_ERROR_INVALID_PARAM
if the interface does not exist
LIBUSB_ERROR_NO_DEVICE
if the device has been disconnected
LIBUSB_ERROR_NOT_SUPPORTED
on platforms where the functionality is not available
LIBUSB_ERROR_BUSY
if the driver cannot be attached because the interface is claimed by a program or driver
another LIBUSB_ERROR code on other failure
-
See also
-
libusb_kernel_driver_active()
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.
-
Parameters
-
dev_handlea device handle
enablewhether to enable or disable auto kernel driver detachment
-
Returns
-
LIBUSB_SUCCESS
on success
LIBUSB_ERROR_NOT_SUPPORTED
on platforms where the functionality is not available
-
See also
-
libusb_claim_interface()
libusb_release_interface()
libusb_set_configuration()