My Project
A cross-platform user library to access USB devices
Device hotplug event notification

Introduction

Version 1.0.16, LIBUSB_API_VERSION >= 0x01000102, has added support for hotplug events on some platforms (you should test if your platform supports hotplug notification by calling libusb_has_capability() with parameter LIBUSB_CAP_HAS_HOTPLUG).

This interface allows you to request notification for the arrival and departure of matching USB devices.

To receive hotplug notification you register a callback by calling libusb_hotplug_register_callback(). This function will optionally return a callback handle that can be passed to libusb_hotplug_deregister_callback().

A callback function must return an int (0 or 1) indicating whether the callback is expecting additional events. Returning 0 will rearm the callback and 1 will cause the callback to be deregistered. Note that when callbacks are called from libusb_hotplug_register_callback() because of the LIBUSB_HOTPLUG_ENUMERATE flag, the callback return value is ignored, iow you cannot cause a callback to be deregistered by returning 1 when it is called from libusb_hotplug_register_callback().

Callbacks for a particular context are automatically deregistered by libusb_exit().

As of 1.0.16 there are two supported hotplug events:

  • LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: A device has arrived and is ready to use
  • LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: A device has left and is no longer available

A hotplug event can listen for either or both of these events.

Note: If you receive notification that a device has left and you have any a libusb_device_handles for the device it is up to you to call libusb_close() on each device handle to free up any remaining resources associated with the device. Once a device has left any libusb_device_handle associated with the device are invalid and will remain so even if the device comes back.

When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event it is considered safe to call any libusb function that takes a libusb_device. It also safe to open a device and submit asynchronous transfers. However, most other functions that take a libusb_device_handle are not safe to call. Examples of such functions are any of the synchronous API functions or the blocking functions that retrieve various USB descriptors. These functions must be used outside of the context of the hotplug callback.

When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function is libusb_get_device_descriptor().

The following code provides an example of the usage of the hotplug interface:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <libusb.h>
static int count = 0;
int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
libusb_hotplug_event event, void *user_data) {
static libusb_device_handle *dev_handle = NULL;
int rc;
(void)libusb_get_device_descriptor(dev, &desc);
rc = libusb_open(dev, &dev_handle);
if (LIBUSB_SUCCESS != rc) {
printf("Could not open USB device\n");
}
} else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
if (dev_handle) {
libusb_close(dev_handle);
dev_handle = NULL;
}
} else {
printf("Unhandled event %d\n", event);
}
count++;
return 0;
}
int main (void) {
int rc;
libusb_init(NULL);
LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,
&callback_handle);
if (LIBUSB_SUCCESS != rc) {
printf("Error creating a hotplug callback\n");
libusb_exit(NULL);
return EXIT_FAILURE;
}
while (count < 2) {
nanosleep(&(struct timespec){0, 10000000UL}, NULL);
}
libusb_hotplug_deregister_callback(NULL, callback_handle);
libusb_exit(NULL);
return 0;
}
libusb_get_device_descriptor
int libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc)
Definition: descriptor.c:543
libusb_hotplug_event
libusb_hotplug_event
Definition: libusb.h:1925
libusb_context
struct libusb_context libusb_context
Definition: libusb.h:939
libusb_exit
void libusb_exit(struct libusb_context *ctx)
Definition: core.c:2364
LIBUSB_HOTPLUG_MATCH_ANY
#define LIBUSB_HOTPLUG_MATCH_ANY
Definition: libusb.h:1937
libusb_init
int libusb_init(libusb_context **context)
Definition: core.c:2253
libusb_device
struct libusb_device libusb_device
Definition: libusb.h:956
libusb_handle_events_completed
int libusb_handle_events_completed(libusb_context *ctx, int *completed)
Definition: io.c:2487
libusb_hotplug_callback_handle
int libusb_hotplug_callback_handle
Definition: libusb.h:1905
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT
@ LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT
A device has left and is no longer available.
Definition: libusb.h:1932
libusb_device_descriptor
Definition: libusb.h:485
LIBUSB_SUCCESS
@ LIBUSB_SUCCESS
Success (no error)
Definition: libusb.h:1056
libusb_open
int libusb_open(libusb_device *dev, libusb_device_handle **dev_handle)
Definition: core.c:1325
LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
@ LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
A device has been plugged in and is ready to use.
Definition: libusb.h:1927
libusb_hotplug_register_callback
int libusb_hotplug_register_callback(libusb_context *ctx, libusb_hotplug_event events, libusb_hotplug_flag flags, int vendor_id, int product_id, int dev_class, libusb_hotplug_callback_fn cb_fn, void *user_data, libusb_hotplug_callback_handle *callback_handle)
Definition: hotplug.c:234
libusb_hotplug_deregister_callback
void libusb_hotplug_deregister_callback(struct libusb_context *ctx, libusb_hotplug_callback_handle callback_handle)
Definition: hotplug.c:322
libusb_close
void libusb_close(libusb_device_handle *dev_handle)
Definition: core.c:1487
libusb_device_handle
struct libusb_device_handle libusb_device_handle
Definition: libusb.h:967