diff --git a/usb/config.go b/usb/config.go index 41142ac..b36c72c 100644 --- a/usb/config.go +++ b/usb/config.go @@ -27,6 +27,8 @@ type EndpointInfo struct { // Number represents the endpoint number. Note that the endpoint number is different from the // address field in the descriptor - address 0x82 means endpoint number 2, // with endpoint direction IN. + // The device can have up to two endpoints with the same number but with + // different directions. Number uint8 // Direction defines whether the data is flowing IN or OUT from the host perspective. Direction EndpointDirection @@ -44,9 +46,9 @@ type EndpointInfo struct { UsageType UsageType } -func (e EndpointInfo) addr() uint8 { - addr := e.Number - if e.Direction == EndpointDirectionIn { +func endpointAddr(n uint8, d EndpointDirection) uint8 { + addr := n + if d == EndpointDirectionIn { addr |= 0x80 } return addr @@ -55,7 +57,7 @@ func (e EndpointInfo) addr() uint8 { // String returns the human-readable description of the endpoint. func (e EndpointInfo) String() string { ret := make([]string, 0, 3) - ret = append(ret, fmt.Sprintf("Endpoint #%d %s (address 0x%02x) %s", e.Number, e.Direction, e.addr(), e.TransferType)) + ret = append(ret, fmt.Sprintf("Endpoint #%d %s (address 0x%02x) %s", e.Number, e.Direction, endpointAddr(e.Number, e.Direction), e.TransferType)) switch e.TransferType { case TransferTypeIsochronous: ret = append(ret, fmt.Sprintf("- %s %s", e.IsoSyncType, e.UsageType)) diff --git a/usb/device.go b/usb/device.go index 5937dde..da90086 100644 --- a/usb/device.go +++ b/usb/device.go @@ -92,7 +92,7 @@ func (d *Device) Close() error { return nil } -func (d *Device) OpenEndpoint(epNum, cfgNum, ifNum, setNum uint8) (*Endpoint, error) { +func (d *Device) OpenEndpoint(epAddr, cfgNum, ifNum, setNum uint8) (*Endpoint, error) { var cfg *ConfigInfo for _, c := range d.Configs { if c.Config == cfgNum { @@ -102,7 +102,7 @@ func (d *Device) OpenEndpoint(epNum, cfgNum, ifNum, setNum uint8) (*Endpoint, er } } if cfg == nil { - return nil, fmt.Errorf("usb: unknown configuration %02x", cfgNum) + return nil, fmt.Errorf("usb: unknown configuration 0x%02x", cfgNum) } var intf *InterfaceInfo @@ -114,7 +114,7 @@ func (d *Device) OpenEndpoint(epNum, cfgNum, ifNum, setNum uint8) (*Endpoint, er } } if intf == nil { - return nil, fmt.Errorf("usb: unknown interface %02x", ifNum) + return nil, fmt.Errorf("usb: unknown interface 0x%02x", ifNum) } var setAlternate bool @@ -127,18 +127,18 @@ func (d *Device) OpenEndpoint(epNum, cfgNum, ifNum, setNum uint8) (*Endpoint, er } } if ifs == nil { - return nil, fmt.Errorf("usb: unknown setup %02x", setNum) + return nil, fmt.Errorf("usb: unknown setup 0x%02x", setNum) } var ep *EndpointInfo for _, e := range ifs.Endpoints { - if e.Number == epNum { - debug.Printf("found ep %02x in %#v\n", epNum, *ifs) + if endpointAddr(e.Number, e.Direction) == epAddr { + debug.Printf("found ep 0x%02x %s in %#v\n", e.Number, e.Direction, *ifs) ep = &e } } if ep == nil { - return nil, fmt.Errorf("usb: unknown endpoint %02x", epNum) + return nil, fmt.Errorf("usb: didn't find endpoint address 0x%02x", epAddr) } end := newEndpoint(d.handle, *ifs, *ep, d.ReadTimeout, d.WriteTimeout) diff --git a/usb/endpoint_test.go b/usb/endpoint_test.go index bda4115..cda549c 100644 --- a/usb/endpoint_test.go +++ b/usb/endpoint_test.go @@ -120,7 +120,7 @@ func TestOpenEndpoint(t *testing.T) { if err != nil { t.Fatalf("OpenDeviceWithVidPid(0x8888, 0x0002): got error %v, want nil", err) } - got, err := dev.OpenEndpoint(6, 1, 1, 2) + got, err := dev.OpenEndpoint(0x86, 1, 1, 2) if err != nil { t.Fatalf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got error %v, want nil", err) } diff --git a/usb/libusb.go b/usb/libusb.go index 142f0b8..e6b2ccb 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -374,7 +374,7 @@ func (libusbImpl) alloc(d *libusbDevHandle, ep *EndpointInfo, timeout time.Durat return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets) } xfer.dev_handle = (*C.libusb_device_handle)(d) - xfer.endpoint = C.uchar(ep.addr()) + xfer.endpoint = C.uchar(endpointAddr(ep.Number, ep.Direction)) xfer.timeout = C.uint(timeout / time.Millisecond) xfer._type = C.uchar(ep.TransferType) xfer.num_iso_packets = C.int(isoPackets)