Typedefs | |
| typedef struct libusb_device | libusb_device |
| Structure representing a USB device detected on the system. | |
| typedef struct libusb_device_handle | libusb_device_handle |
| Structure representing a handle on a USB device. | |
Functions | |
| ssize_t | libusb_get_device_list (libusb_context *ctx, libusb_device ***list) |
| Returns a list of USB devices currently attached to the system. | |
| void | libusb_free_device_list (libusb_device **list, int unref_devices) |
| Frees a list of devices previously discovered using libusb_get_device_list(). | |
| uint8_t | libusb_get_bus_number (libusb_device *dev) |
| Get the number of the bus that a device is connected to. | |
| uint8_t | libusb_get_device_address (libusb_device *dev) |
| Get the address of the device on the bus it is connected to. | |
| 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. | |
| 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. | |
| libusb_device * | libusb_ref_device (libusb_device *dev) |
| Increment the reference count of a device. | |
| void | libusb_unref_device (libusb_device *dev) |
| Decrement the reference count of a device. | |
| int | libusb_open (libusb_device *dev, libusb_device_handle **handle) |
| Open a device and obtain a device handle. | |
| 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. | |
| void | libusb_close (libusb_device_handle *dev_handle) |
| Close a device handle. | |
| libusb_device * | libusb_get_device (libusb_device_handle *dev_handle) |
| Get the underlying device for a handle. | |
| int | libusb_get_configuration (libusb_device_handle *dev, int *config) |
| Determine the bConfigurationValue of the currently active configuration. | |
| int | libusb_set_configuration (libusb_device_handle *dev, int configuration) |
| Set the active configuration for a device. | |
| int | libusb_claim_interface (libusb_device_handle *dev, int interface_number) |
| Claim an interface on a given device handle. | |
| int | libusb_release_interface (libusb_device_handle *dev, int interface_number) |
| Release an interface previously claimed with libusb_claim_interface(). | |
| int | libusb_set_interface_alt_setting (libusb_device_handle *dev, int interface_number, int alternate_setting) |
| Activate an alternate setting for an interface. | |
| int | libusb_clear_halt (libusb_device_handle *dev, unsigned char endpoint) |
| Clear the halt/stall condition for an endpoint. | |
| int | libusb_reset_device (libusb_device_handle *dev) |
| Perform a USB port reset to reinitialize a device. | |
| int | libusb_kernel_driver_active (libusb_device_handle *dev, int interface) |
| Determine if a kernel driver is active on an interface. | |
| int | libusb_detach_kernel_driver (libusb_device_handle *dev, int interface) |
| Detach a kernel driver from an interface. | |
| int | libusb_attach_kernel_driver (libusb_device_handle *dev, int interface) |
| Re-attach an interface's kernel driver, which was previously detached using libusb_detach_kernel_driver(). | |
// discover devices libusb_device **list; libusb_device *found = NULL; ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t i = 0; int err = 0; if (cnt < 0) error(); for (i = 0; i < cnt; i++) { libusb_device *device = list[i]; if (is_interesting(device)) { found = device; break; } } if (found) { libusb_device_handle *handle; err = libusb_open(found, &handle); if (err) error(); // etc } libusb_free_device_list(list, 1);
The two important points:
If you ended up with a handle, you can now proceed to perform I/O on the device.
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.
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.
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().
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_device_ref() and libusb_device_unref(), 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().
| 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 | ||
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | | |
‹/td>| list | the list to free | |