From bb193503d4066d38bc40a46e603bdc5909e7c243 Mon Sep 17 00:00:00 2001 From: Juan <994594+juaoose@users.noreply.github.com> Date: Wed, 15 May 2024 13:23:47 -0500 Subject: [PATCH 1/2] Add unknown interface descriptors to Extra --- interface.go | 2 ++ libusb.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/interface.go b/interface.go index 0af77a9..8439ffa 100644 --- a/interface.go +++ b/interface.go @@ -51,6 +51,8 @@ type InterfaceSetting struct { // Endpoints enumerates the endpoints available on this interface with // this alternate setting. Endpoints map[EndpointAddress]EndpointDesc + // Unknown interface descriptors + Extra []byte iInterface int // index of a string descriptor describing this interface. } diff --git a/libusb.go b/libusb.go index ddc0b2a..76937f2 100644 --- a/libusb.go +++ b/libusb.go @@ -301,12 +301,14 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) { } descs := make([]InterfaceSetting, 0, len(alts)) for _, alt := range alts { + i := InterfaceSetting{ Number: int(alt.bInterfaceNumber), Alternate: int(alt.bAlternateSetting), Class: Class(alt.bInterfaceClass), SubClass: Class(alt.bInterfaceSubClass), Protocol: Protocol(alt.bInterfaceProtocol), + Extra: C.GoBytes(unsafe.Pointer(alt.extra), alt.extra_length), iInterface: int(alt.iInterface), } From 965a4971f089eb3d86106bfc0426ed1e22ed6d7b Mon Sep 17 00:00:00 2001 From: Juan <994594+juaoose@users.noreply.github.com> Date: Thu, 16 May 2024 20:01:23 -0500 Subject: [PATCH 2/2] Update after comments --- constants.go | 19 ++++++++++--------- interface.go | 10 ++++++++-- libusb.go | 27 ++++++++++++++++++++------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/constants.go b/constants.go index aa1ce40..a301aad 100644 --- a/constants.go +++ b/constants.go @@ -94,15 +94,16 @@ type DescriptorType uint8 // Descriptor types defined by the USB spec. const ( - DescriptorTypeDevice DescriptorType = C.LIBUSB_DT_DEVICE - DescriptorTypeConfig DescriptorType = C.LIBUSB_DT_CONFIG - DescriptorTypeString DescriptorType = C.LIBUSB_DT_STRING - DescriptorTypeInterface DescriptorType = C.LIBUSB_DT_INTERFACE - DescriptorTypeEndpoint DescriptorType = C.LIBUSB_DT_ENDPOINT - DescriptorTypeHID DescriptorType = C.LIBUSB_DT_HID - DescriptorTypeReport DescriptorType = C.LIBUSB_DT_REPORT - DescriptorTypePhysical DescriptorType = C.LIBUSB_DT_PHYSICAL - DescriptorTypeHub DescriptorType = C.LIBUSB_DT_HUB + DescriptorTypeDevice DescriptorType = C.LIBUSB_DT_DEVICE + DescriptorTypeConfig DescriptorType = C.LIBUSB_DT_CONFIG + DescriptorTypeString DescriptorType = C.LIBUSB_DT_STRING + DescriptorTypeInterface DescriptorType = C.LIBUSB_DT_INTERFACE + DescriptorTypeCSInterface DescriptorType = 0x24 + DescriptorTypeEndpoint DescriptorType = C.LIBUSB_DT_ENDPOINT + DescriptorTypeHID DescriptorType = C.LIBUSB_DT_HID + DescriptorTypeReport DescriptorType = C.LIBUSB_DT_REPORT + DescriptorTypePhysical DescriptorType = C.LIBUSB_DT_PHYSICAL + DescriptorTypeHub DescriptorType = C.LIBUSB_DT_HUB ) var descriptorTypeDescription = map[DescriptorType]string{ diff --git a/interface.go b/interface.go index 8439ffa..0ae125f 100644 --- a/interface.go +++ b/interface.go @@ -35,6 +35,11 @@ func (i InterfaceDesc) String() string { return fmt.Sprintf("Interface %d (%d alternate settings)", i.Number, len(i.AltSettings)) } +type RawDescriptor struct { + Type DescriptorType + Data []byte +} + // InterfaceSetting contains information about a USB interface with a particular // alternate setting, extracted from the descriptor. type InterfaceSetting struct { @@ -51,8 +56,9 @@ type InterfaceSetting struct { // Endpoints enumerates the endpoints available on this interface with // this alternate setting. Endpoints map[EndpointAddress]EndpointDesc - // Unknown interface descriptors - Extra []byte + // Extra interface descriptors, these are descriptors that are not part of the USB spec + // Potential examples are Class-Specific VC Interface Descriptor (UVC spec) + ExtraDescriptors []*RawDescriptor iInterface int // index of a string descriptor describing this interface. } diff --git a/libusb.go b/libusb.go index 76937f2..b380ea5 100644 --- a/libusb.go +++ b/libusb.go @@ -302,14 +302,27 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) { descs := make([]InterfaceSetting, 0, len(alts)) for _, alt := range alts { + var extraDescriptors []*RawDescriptor + extra := C.GoBytes(unsafe.Pointer(alt.extra), alt.extra_length) + for i := 0; i != len(extra); i += int(extra[i]) { + block := extra[i : i+int(extra[i])] + bType := DescriptorType(block[1]) + + extraDescriptors = append(extraDescriptors, + &RawDescriptor{ + Type: bType, + Data: block}, + ) + } + i := InterfaceSetting{ - Number: int(alt.bInterfaceNumber), - Alternate: int(alt.bAlternateSetting), - Class: Class(alt.bInterfaceClass), - SubClass: Class(alt.bInterfaceSubClass), - Protocol: Protocol(alt.bInterfaceProtocol), - Extra: C.GoBytes(unsafe.Pointer(alt.extra), alt.extra_length), - iInterface: int(alt.iInterface), + Number: int(alt.bInterfaceNumber), + Alternate: int(alt.bAlternateSetting), + Class: Class(alt.bInterfaceClass), + SubClass: Class(alt.bInterfaceSubClass), + Protocol: Protocol(alt.bInterfaceProtocol), + ExtraDescriptors: extraDescriptors, + iInterface: int(alt.iInterface), } if hasIntf[i.Number][i.Alternate] {