From 9954f88a0df2f903aface3b887992b5c4b36d4b3 Mon Sep 17 00:00:00 2001 From: Matthew Di Ferrante Date: Tue, 6 Sep 2016 14:44:02 +0100 Subject: [PATCH] fix walker such that it returns 1 device per interface --- example/main.go | 4 ++-- hid.go | 2 -- usb_linux.go | 18 +++++++++--------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/example/main.go b/example/main.go index 471a4f7..49fe9ce 100644 --- a/example/main.go +++ b/example/main.go @@ -120,7 +120,7 @@ func main() { found := false hid.UsbWalk(func(device hid.Device) { info := device.Info() - fmt.Printf("%04x:%04x:%04x\n", info.Vendor, info.Product, info.Revision) + fmt.Printf("%04x:%04x:%04x:%02x\n", info.Vendor, info.Product, info.Revision, info.Interface) found = true }) if !found { @@ -131,7 +131,7 @@ func main() { hid.UsbWalk(func(device hid.Device) { info := device.Info() - id := fmt.Sprintf("%04x:%04x:%04x", info.Vendor, info.Product, info.Revision) + id := fmt.Sprintf("%04x:%04x:%04x:%02x", info.Vendor, info.Product, info.Revision, info.Interface) if id != os.Args[1] { return } diff --git a/hid.go b/hid.go index 480c984..33f922e 100644 --- a/hid.go +++ b/hid.go @@ -23,8 +23,6 @@ type Device interface { Open() error Close() Info() Info - SetEndpoint(int) - SetInterface(int) HIDReport() ([]byte, error) SetReport(int, []byte) error GetReport(int) ([]byte, error) diff --git a/usb_linux.go b/usb_linux.go index b45f227..35efe79 100644 --- a/usb_linux.go +++ b/usb_linux.go @@ -27,15 +27,6 @@ type usbDevice struct { path string } -func (hid *usbDevice) SetEndpoint(ep int) { - hid.epOut = ep - hid.epIn = ep + 0x80 -} - -func (hid *usbDevice) SetInterface(ifno int) { - hid.info.Interface = uint8(ifno) -} - func (hid *usbDevice) Open() (err error) { if hid.f != nil { return errors.New("device is already opened") @@ -238,6 +229,10 @@ func walker(path string, cb func(Device)) error { device = nil } case UsbDescTypeInterface: + if device != nil { + cb(device) + device = nil + } expected[UsbDescTypeEndpoint] = true expected[UsbDescTypeReport] = true i := &interfaceDesc{} @@ -259,6 +254,11 @@ func walker(path string, cb func(Device)) error { } case UsbDescTypeEndpoint: if device != nil { + if device.epIn != 0 && device.epOut != 0 { + cb(device) + device.epIn = 0 + device.epOut = 0 + } e := &endpointDesc{} if err := cast(body, e); err != nil { return err