From a7f2e81ddffe8991d215a00b1d019227d7d778e1 Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Thu, 16 Feb 2017 23:41:52 +0800 Subject: [PATCH 1/5] detach before claim --- usb/device.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/usb/device.go b/usb/device.go index 6100a79..b396a88 100644 --- a/usb/device.go +++ b/usb/device.go @@ -174,6 +174,11 @@ 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)) From c84750a9d7c3a8dd72e862e13719dcdda8d13332 Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Fri, 17 Feb 2017 01:05:34 +0800 Subject: [PATCH 2/5] 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 +} From 16dcc9999731c3dafa3227b78cb18cedb465d13e Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Fri, 17 Feb 2017 01:19:33 +0800 Subject: [PATCH 3/5] add bool handling --- usb/device.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/usb/device.go b/usb/device.go index 7d8bbb2..b707e4e 100644 --- a/usb/device.go +++ b/usb/device.go @@ -222,7 +222,12 @@ func (d *Device) GetStringDescriptor(desc_index int) (string, error) { // 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 { +func (d *Device) SetAutoDetach(autodetach bool) error { + autodetachInt := 0 + if autodetach { + autodetachInt = 1 + } + errno := C.libusb_set_auto_detach_kernel_driver( d.handle, C.int(autodetach), From b13a99c292eb30f4271e734a174512803d885dcf Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Fri, 17 Feb 2017 01:24:57 +0800 Subject: [PATCH 4/5] improve comment --- usb/device.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/usb/device.go b/usb/device.go index b707e4e..e588c85 100644 --- a/usb/device.go +++ b/usb/device.go @@ -219,8 +219,8 @@ func (d *Device) GetStringDescriptor(desc_index int) (string, error) { } // 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. +// When autodetach is enabled libusb will automatically detach the kernel driver +// on the interface and reattach it when releasing the interface. // Automatic kernel driver detachment is disabled on newly opened device handles by default. func (d *Device) SetAutoDetach(autodetach bool) error { autodetachInt := 0 From 56fd0d1199e17b7eabde11c4d41ab4b70c9f1d69 Mon Sep 17 00:00:00 2001 From: vincentserpoul Date: Fri, 17 Feb 2017 01:33:51 +0800 Subject: [PATCH 5/5] error handling and bool handling --- usb/device.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/usb/device.go b/usb/device.go index e588c85..b5c3d3e 100644 --- a/usb/device.go +++ b/usb/device.go @@ -228,15 +228,15 @@ func (d *Device) SetAutoDetach(autodetach bool) error { autodetachInt = 1 } - errno := C.libusb_set_auto_detach_kernel_driver( + err := C.libusb_set_auto_detach_kernel_driver( d.handle, - C.int(autodetach), + C.int(autodetachInt), ) // TODO LIBUSB_ERROR_NOT_SUPPORTED (-12) handling // if any errors occur - if errno < 0 { - return fmt.Errorf("usb: setautodetach: %s", usbError(errno)) + if err != C.int(SUCCESS) { + return fmt.Errorf("usb: setautodetach: %s", usbError(err)) } return nil }