Skip to content

Commit fa75ffb

Browse files
mayanksumanYouw
authored andcommitted
Reattach kernel driver in hidapi-libusb
1 parent dd16e7c commit fa75ffb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

libusb/hid.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ struct hid_device_ {
173173

174174
/* List of received input reports. */
175175
struct input_report *input_reports;
176+
177+
/* Was kernel driver detached by libusb */
178+
#ifdef DETACH_KERNEL_DRIVER
179+
int is_driver_detached;
180+
#endif
176181
};
177182

178183
static libusb_context *usb_context = NULL;
@@ -908,6 +913,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
908913
#ifdef DETACH_KERNEL_DRIVER
909914
/* Detach the kernel driver, but only if the
910915
device is managed by the kernel */
916+
dev->is_driver_detached = 0;
911917
if (libusb_kernel_driver_active(dev->device_handle, intf_desc->bInterfaceNumber) == 1) {
912918
res = libusb_detach_kernel_driver(dev->device_handle, intf_desc->bInterfaceNumber);
913919
if (res < 0) {
@@ -917,6 +923,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
917923
good_open = 0;
918924
break;
919925
}
926+
else {
927+
dev->is_driver_detached = 1;
928+
LOG("Driver successfully detached from kernel.\n");
929+
}
920930
}
921931
#endif
922932
res = libusb_claim_interface(dev->device_handle, intf_desc->bInterfaceNumber);
@@ -1247,6 +1257,15 @@ void HID_API_EXPORT hid_close(hid_device *dev)
12471257
/* release the interface */
12481258
libusb_release_interface(dev->device_handle, dev->interface);
12491259

1260+
/* reattach the kernel driver if it was detached */
1261+
#ifdef DETACH_KERNEL_DRIVER
1262+
if (dev->is_driver_detached) {
1263+
int res = libusb_attach_kernel_driver(dev->device_handle, dev->interface);
1264+
if (res < 0)
1265+
LOG("Failed to reattach the driver to kernel.\n");
1266+
}
1267+
#endif
1268+
12501269
/* Close the handle */
12511270
libusb_close(dev->device_handle);
12521271

0 commit comments

Comments
 (0)