From c84750a9d7c3a8dd72e862e13719dcdda8d13332 Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Fri, 17 Feb 2017 01:05:34 +0800 Subject: [PATCH] remove systematic detach, add SetAutoDetach method --- usb/device.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/usb/device.go b/usb/device.go index b396a88..7d8bbb2 100644 --- a/usb/device.go +++ b/usb/device.go @@ -174,11 +174,6 @@ found: } } - // Detach the interface - if errno := C.libusb_detach_kernel_driver(d.handle, C.int(iface)); errno < 0 { - fmt.Errorf("usb: detach: %s", usbError(errno)) - } - // Claim the interface if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 { return nil, fmt.Errorf("usb: claim: %s", usbError(errno)) @@ -222,3 +217,21 @@ func (d *Device) GetStringDescriptor(desc_index int) (string, error) { return stringDescriptor, nil } + +// SetAutoDetach Enable/disable libusb's automatic kernel driver detachment. +// When this is enabled libusb will automatically detach the kernel driver +// on an interface when claiming the interface, and attach it when releasing the interface. +// Automatic kernel driver detachment is disabled on newly opened device handles by default. +func (d *Device) SetAutoDetach(autodetach int) error { + errno := C.libusb_set_auto_detach_kernel_driver( + d.handle, + C.int(autodetach), + ) + + // TODO LIBUSB_ERROR_NOT_SUPPORTED (-12) handling + // if any errors occur + if errno < 0 { + return fmt.Errorf("usb: setautodetach: %s", usbError(errno)) + } + return nil +}