libusb-1.0 API Reference

Introduction

libusb is an open source library that allows you to communicate with USB devices from userspace. For more info, see the libusb homepage.

This documentation is aimed at application developers wishing to communicate with USB peripherals from their own software. After reviewing this documentation, feedback and questions can be sent to the libusb-devel mailing list.

This documentation assumes knowledge of how to operate USB devices from a software standpoint (descriptors, configurations, interfaces, endpoints, control/bulk/interrupt/isochronous transfers, etc). Full information can be found in the USB 3.0 Specification which is available for free download. You can probably find less verbose introductions by searching the web.

Application Programming Interface (API)

See the Application Programming Interface page for a complete list of the libusb functions.

Library features

  • All transfer types supported (control/bulk/interrupt/isochronous)

  • 2 transfer interfaces:

    1. Synchronous (simple)

    2. Asynchronous (more complicated, but more powerful)

  • Thread safe (although the asynchronous interface means that you usually won’t need to thread)

  • Lightweight with lean API

  • Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer

  • Hotplug support (on some platforms). See Device hotplug event notification.

Getting Started

To begin reading the API documentation, start with the Modules page which links to the different categories of libusb’s functionality.

One decision you will have to make is whether to use the synchronous or the asynchronous data transfer interface. The Synchronous and asynchronous device I/O documentation provides some insight into this topic.

Some example programs can be found in the libusb source distribution under the “examples” subdirectory. The libusb homepage includes a list of real-life project examples which use libusb.

Error handling

libusb functions typically return 0 on success or a negative error code on failure. These negative error codes relate to LIBUSB_ERROR constants which are listed on the miscellaneous documentation page.

Debug message logging

libusb uses stderr for all logging. By default, logging is set to NONE, which means that no output will be produced. However, unless the library has been compiled with logging disabled, then any application calls to libusb_set_debug(), or the setting of the environmental variable LIBUSB_DEBUG outside of the application, can result in logging being produced. Your application should therefore not close stderr, but instead direct it to the null device if its output is undesirable.

The libusb_set_debug() function can be used to enable logging of certain messages. Under standard configuration, libusb doesn’t really log much so you are advised to use this function to enable all error/warning/ informational messages. It will help debug problems with your software.

The logged messages are unstructured. There is no one-to-one correspondence between messages being logged and success or failure return codes from libusb functions. There is no format to the messages, so you should not try to capture or parse them. They are not and will not be localized. These messages are not intended to being passed to your application user; instead, you should interpret the error codes returned from libusb functions and provide appropriate notification to the user. The messages are simply there to aid you as a programmer, and if you’re confused because you’re getting a strange error code from a libusb function, enabling message logging may give you a suitable explanation.

The LIBUSB_DEBUG environment variable can be used to enable message logging at run-time. This environment variable should be set to a log level number, which is interpreted the same as the libusb_set_debug() parameter. When this environment variable is set, the message logging verbosity level is fixed and libusb_set_debug() effectively does nothing.

libusb can be compiled without any logging functions, useful for embedded systems. In this case, libusb_set_debug() and the LIBUSB_DEBUG environment variable have no effects.

libusb can also be compiled with verbose debugging messages always. When the library is compiled in this way, all messages of all verbosities are always logged. libusb_set_debug() and the LIBUSB_DEBUG environment variable have no effects.

Other remarks

libusb does have imperfections. The caveats page attempts to document these.

Further Reading:

Related Pages:

Reference and Index: