@@ -173,6 +173,11 @@ struct hid_device_ {
173
173
174
174
/* List of received input reports. */
175
175
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
176
181
};
177
182
178
183
static libusb_context * usb_context = NULL ;
@@ -908,6 +913,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
908
913
#ifdef DETACH_KERNEL_DRIVER
909
914
/* Detach the kernel driver, but only if the
910
915
device is managed by the kernel */
916
+ dev -> is_driver_detached = 0 ;
911
917
if (libusb_kernel_driver_active (dev -> device_handle , intf_desc -> bInterfaceNumber ) == 1 ) {
912
918
res = libusb_detach_kernel_driver (dev -> device_handle , intf_desc -> bInterfaceNumber );
913
919
if (res < 0 ) {
@@ -917,6 +923,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
917
923
good_open = 0 ;
918
924
break ;
919
925
}
926
+ else {
927
+ dev -> is_driver_detached = 1 ;
928
+ LOG ("Driver successfully detached from kernel.\n" );
929
+ }
920
930
}
921
931
#endif
922
932
res = libusb_claim_interface (dev -> device_handle , intf_desc -> bInterfaceNumber );
@@ -1247,6 +1257,15 @@ void HID_API_EXPORT hid_close(hid_device *dev)
1247
1257
/* release the interface */
1248
1258
libusb_release_interface (dev -> device_handle , dev -> interface );
1249
1259
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
+
1250
1269
/* Close the handle */
1251
1270
libusb_close (dev -> device_handle );
1252
1271
0 commit comments