Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about CSEndpoint?

also add a comment to specify where the value was sourced from. (in this case I assume this is CDC spec v1.1/1.2?), since this descriptor type is not a part of the USB spec as such.

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{
Expand Down
8 changes: 8 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the type. In particular whether the header of the descriptor (type+length) is a part of the data is an important information here.

Type DescriptorType
Data []byte
}

// InterfaceSetting contains information about a USB interface with a particular
// alternate setting, extracted from the descriptor.
type InterfaceSetting struct {
Expand All @@ -51,6 +56,9 @@ type InterfaceSetting struct {
// Endpoints enumerates the endpoints available on this interface with
// this alternate setting.
Endpoints map[EndpointAddress]EndpointDesc
// 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.
}
Expand Down
27 changes: 21 additions & 6 deletions libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,28 @@ 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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join the line for append (i.e. the &RawDescriptor literal starts in the same line)

&RawDescriptor{
Type: bType,
Data: block},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closing curly brace on the next line, together with closing parenthesis

)
}

Comment on lines +305 to +317
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract to a helper function, since we're going to use that on interfaces as well as endpoints and configs.

i := InterfaceSetting{
Number: int(alt.bInterfaceNumber),
Alternate: int(alt.bAlternateSetting),
Class: Class(alt.bInterfaceClass),
SubClass: Class(alt.bInterfaceSubClass),
Protocol: Protocol(alt.bInterfaceProtocol),
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] {
Expand Down