* Also remove Linux special case from xusb sample.
* Note that LIBUSBX_API_VERSION is incremented as a result of
libusb_has_capability() returning nonzero rather than 1 when
a capability is supported.
* A LIBUSB_CAP_HAS_HOTPLUG is also added, though it is currently
not implemented on any platform
* Closes #102
const struct libusb_endpoint_descriptor *endpoint;
int i, j, k, r;
int iface, nb_ifaces, first_iface = -1;
-#if defined(__linux__)
- // Attaching/detaching the kernel driver is only relevant for Linux
+ // For attaching/detaching the kernel driver, if needed
int iface_detached = -1;
-#endif
struct libusb_device_descriptor dev_desc;
const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
"480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"};
{
printf("\nClaiming interface %d...\n", iface);
r = libusb_claim_interface(handle, iface);
-#if defined(__linux__)
- if ((r != LIBUSB_SUCCESS) && (iface == 0)) {
- // Maybe we need to detach the driver
- perr(" Failed. Trying to detach driver...\n");
- libusb_detach_kernel_driver(handle, iface);
- iface_detached = iface;
- printf(" Claiming interface again...\n");
- r = libusb_claim_interface(handle, iface);
+ if ((r != LIBUSB_SUCCESS) && libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)
+ && (libusb_kernel_driver_active(handle, iface) > 0)) {
+ // Try to detach the kernel driver
+ perr(" A kernel driver is active, trying to detach it...\n");
+ r = libusb_detach_kernel_driver(handle, iface);
+ if (r == LIBUSB_SUCCESS) {
+ iface_detached = iface;
+ printf(" Claiming interface again...\n");
+ r = libusb_claim_interface(handle, iface);
+ }
}
-#endif
if (r != LIBUSB_SUCCESS) {
perr(" Failed.\n");
}
libusb_release_interface(handle, iface);
}
-#if defined(__linux__)
if (iface_detached >= 0) {
printf("Re-attaching kernel driver...\n");
libusb_attach_kernel_driver(handle, iface_detached);
}
-#endif
printf("Closing device...\n");
libusb_close(handle);
/** \ingroup misc
* Check at runtime if the loaded library has a given capability.
+ * This call should be performed after \ref libusb_init(), to ensure the
+ * backend has updated its capability set.
*
* \param capability the \ref libusb_capability to check for
- * \returns 1 if the running library has the capability, 0 otherwise
+ * \returns nonzero if the running library has the capability, 0 otherwise
*/
int API_EXPORTED libusb_has_capability(uint32_t capability)
{
switch (capability) {
case LIBUSB_CAP_HAS_CAPABILITY:
return 1;
+ case LIBUSB_CAP_HAS_HID_ACCESS:
+ return (usbi_backend->caps && USBI_CAP_HAS_HID_ACCESS);
+ case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
+ return (usbi_backend->caps && USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
}
return 0;
}
* Internally, LIBUSBX_API_VERSION is defined as follows:
* (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental)
*/
-#define LIBUSBX_API_VERSION 0x010000FF
+#define LIBUSBX_API_VERSION 0x01000101
#ifdef __cplusplus
extern "C" {
*/
enum libusb_capability {
/** The libusb_has_capability() API is available. */
- LIBUSB_CAP_HAS_CAPABILITY = 0,
+ LIBUSB_CAP_HAS_CAPABILITY = 0x0000,
+ /** Hotplug support is available. */
+ LIBUSB_CAP_HAS_HOTPLUG = 0x0001,
+ /** The library can access HID devices without requiring user intervention.
+ * Note that before being able to actually access an HID device, you may
+ * still have to call additional libusbx functions such as
+ * \ref libusb_detach_kernel_driver(). */
+ LIBUSB_CAP_HAS_HID_ACCESS = 0x0100,
+ /** The library supports detaching of the default USB driver, using
+ * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
+ LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101
};
/** \ingroup lib
#define USB_MAXINTERFACES 32
#define USB_MAXCONFIG 8
+/* Backend specific capabilities */
+#define USBI_CAP_HAS_HID_ACCESS 0x00010000
+#define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
+
/* The following is used to silence warnings for unused variables */
-#define UNUSED(var) (void)(var)
+#define UNUSED(var) do { (void)(var); } while(0)
struct list_head {
struct list_head *prev, *next;
/* A human-readable name for your backend, e.g. "Linux usbfs" */
const char *name;
+ /* Binary mask for backend specific capabilities */
+ uint32_t caps;
+
/* Perform initialization of your backend. You might use this function
* to determine specific capabilities of the system, allocate required
* data structures for later, etc.
const struct usbi_os_backend darwin_backend = {
.name = "Darwin",
+ .caps = 0,
.init = darwin_init,
.exit = darwin_exit,
.get_device_list = darwin_get_device_list,
const struct usbi_os_backend linux_usbfs_backend = {
.name = "Linux usbfs",
+ .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
.init = op_init,
.exit = NULL,
.get_device_list = op_get_device_list,
const struct usbi_os_backend openbsd_backend = {
"Synchronous OpenBSD backend",
+ 0,
NULL, /* init() */
NULL, /* exit() */
obsd_get_device_list,
const struct usbi_os_backend wince_backend = {
"Windows CE",
+ 0,
wince_init,
wince_exit,
// NB: MSVC6 does not support named initializers.
const struct usbi_os_backend windows_backend = {
"Windows",
+ USBI_CAP_HAS_HID_ACCESS,
windows_init,
windows_exit,
-#define LIBUSB_NANO 10633
+#define LIBUSB_NANO 10634