Caveats

Device resets

The libusb_reset_device() function allows you to reset a device. If your program has to call such a function, it should obviously be aware that the reset will cause device state to change (e.g. register values may be reset).

The problem is that any other program could reset the device your program is working with, at any time. libusb does not offer a mechanism to inform you when this has happened, so if someone else resets your device it will not be clear to your own program why the device state has changed.

Ultimately, this is a limitation of writing drivers in userspace. Separation from the USB stack in the underlying kernel makes it difficult for the operating system to deliver such notifications to your program. The Linux kernel USB stack allows such reset notifications to be delivered to in-kernel USB drivers, but it is not clear how such notifications could be delivered to second-class drivers that live in userspace.

Blocking-only functionality

The functionality listed below is only available through synchronous, blocking functions. There are no asynchronous/non-blocking alternatives, and no clear ways of implementing these.

No hotplugging

libusb-1.0 lacks functionality for providing notifications of when devices are added or removed. This functionality is planned to be implemented for libusb-1.1.

That said, there is basic disconnection handling for open device handles:

Configuration selection and handling

When libusb presents a device handle to an application, there is a chance that the corresponding device may be in unconfigured state. For devices with multiple configurations, there is also a chance that the configuration currently selected is not the one that the application wants to use.

The obvious solution is to add a call to libusb_set_configuration() early on during your device initialization routines, but there are caveats to be aware of:

  1. If the device is already in the desired configuration, calling libusb_set_configuration() using the same configuration value will cause a lightweight device reset. This may not be desirable behaviour.
  2. libusb will be unable to change configuration if the device is in another configuration and other programs or drivers have claimed interfaces under that configuration.
  3. In the case where the desired configuration is already active, libusb may not even be able to perform a lightweight device reset. For example, take my USB keyboard with fingerprint reader: I'm interested in driving the fingerprint reader interface through libusb, but the kernel's USB-HID driver will almost always have claimed the keyboard interface. Because the kernel has claimed an interface, it is not even possible to perform the lightweight device reset, so libusb_set_configuration() will fail. (Luckily the device in question only has a single configuration.)

One solution to some of the above problems is to consider the currently active configuration. If the configuration we want is already active, then we don't have to select any configuration:

cfg = libusb_get_configuration(dev);
if (cfg != desired)
    libusb_set_configuration(dev, desired);

This is probably suitable for most scenarios, but is inherently racy: another application or driver may change the selected configuration after the libusb_get_configuration() call.

Even in cases where libusb_set_configuration() succeeds, consider that other applications or drivers may change configuration after your application calls libusb_set_configuration().

One possible way to lock your device into a specific configuration is as follows:

  1. Set the desired configuration (or use the logic above to realise that it is already in the desired configuration)
  2. Claim the interface that you wish to use
  3. Check that the currently active configuration is the one that you want to use.

The above method works because once an interface is claimed, no application or driver is able to select another configuration.


Generated on Wed Feb 25 16:02:07 2009 for libusb by  doxygen 1.5.8