Documentation
¶
Overview ¶
Package usb contains helpers for building USB descriptors and data.
Index ¶
- Constants
- func EncodeStringDescriptor(s string) []byte
- type ClassSpecificDescriptor
- type ConfigHeader
- type ConfigurationDescriptor
- type ControlDevice
- type Data
- type Descriptor
- type Device
- type DeviceDescriptor
- type EndpointDescriptor
- type HIDDescriptor
- type HIDFunction
- type HIDSubDescriptor
- type InterfaceAssociationDescriptor
- type InterfaceConfig
- type InterfaceDescriptor
- type MicrosoftOS10Descriptor
- func (d MicrosoftOS10Descriptor) CompatibleIDDescriptor() []byte
- func (d MicrosoftOS10Descriptor) ControlResponse(wValue, wIndex uint16) ([]byte, bool)
- func (d MicrosoftOS10Descriptor) EffectiveVendorCode() uint8
- func (d MicrosoftOS10Descriptor) ExtendedPropertiesDescriptor() []byte
- func (d MicrosoftOS10Descriptor) StringDescriptor() []byte
Constants ¶
const ( DeviceDescType = 0x01 ConfigDescType = 0x02 InterfaceDescType = 0x04 EndpointDescType = 0x05 IADDescType = 0x0B HIDDescType = 0x21 ReportDescType = 0x22 )
USB descriptor type constants
const ( DeviceDescLen = 18 ConfigDescLen = 9 IADDescLen = 8 InterfaceDescLen = 9 EndpointDescLen = 7 HIDDescLen = 9 )
Descriptor lengths in bytes (fixed values from USB spec)
Variables ¶
This section is empty.
Functions ¶
func EncodeStringDescriptor ¶
EncodeStringDescriptor converts a UTF-8 string to a USB string descriptor byte array. The resulting descriptor has the format:
Byte 0: bLength (total descriptor length) Byte 1: bDescriptorType (0x03 for string) Bytes 2+: UTF-16LE encoded string
Types ¶
type ClassSpecificDescriptor ¶ added in v0.3.2
ClassSpecificDescriptor represents an opaque class-specific interface descriptor.
It auto-calculates bLength and hardcodes bDescriptorType. Payload contains all bytes after the (bLength,bDescriptorType) header.
func (ClassSpecificDescriptor) Bytes ¶ added in v0.3.2
func (d ClassSpecificDescriptor) Bytes() Data
type ConfigHeader ¶
type ConfigHeader struct {
WTotalLength uint16 // LE, to be patched after building
BNumInterfaces uint8
BConfigurationValue uint8
IConfiguration uint8
BMAttributes uint8
BMaxPower uint8
}
ConfigHeader represents the USB configuration descriptor header (9 bytes).
func (ConfigHeader) Write ¶
func (h ConfigHeader) Write(b *bytes.Buffer)
type ConfigurationDescriptor ¶ added in v0.7.0
type ConfigurationDescriptor struct {
BConfigurationValue uint8
IConfiguration uint8
BMAttributes uint8
BMaxPower uint8
}
ConfigurationDescriptor contains the non-derived fields from the USB configuration descriptor. Zero values keep the server defaults.
type ControlDevice ¶ added in v0.3.2
type ControlDevice interface {
// HandleControl handles a control request.
//
// - bmRequestType, bRequest, wValue, wIndex, wLength are the raw setup packet fields.
// - data is the OUT data stage payload (for host-to-device requests), and is nil for
// device-to-host requests.
//
// If handled is false, the server will fall back to its default behavior.
// If handled is true, the returned bytes (if any) will be used as the IN data stage.
HandleControl(bmRequestType, bRequest uint8, wValue, wIndex, wLength uint16, data []byte) (resp []byte, handled bool)
}
ControlDevice is an optional interface for devices that need to handle control transfers on endpoint 0 (EP0).
This is primarily used for class-specific requests that are not covered by the server's built-in standard request handling (e.g. HID GET_REPORT/ SET_REPORT).
type Descriptor ¶
type Descriptor struct {
Device DeviceDescriptor
Configuration ConfigurationDescriptor
MicrosoftOS10 *MicrosoftOS10Descriptor
Associations []InterfaceAssociationDescriptor
Interfaces []InterfaceConfig
Strings map[uint8]string
}
Descriptor holds all static descriptor/config data for a device.
func (Descriptor) Bytes ¶
func (d Descriptor) Bytes() []byte
Bytes returns the binary representation of the DeviceDescriptor with BLength auto-filled.
func (Descriptor) Interface ¶ added in v0.7.0
func (d Descriptor) Interface(number uint8) (InterfaceConfig, bool)
Interface returns the first matching interface descriptor for an interface number, preferring alternate setting zero when available.
func (Descriptor) NumInterfaces ¶ added in v0.7.0
func (d Descriptor) NumInterfaces() uint8
NumInterfaces returns the number of distinct interface numbers in the active configuration. Alternate settings share the same interface number and only count once in the USB configuration descriptor header.
type Device ¶
type Device interface {
// HandleTransfer processes a non-EP0 transfer (interrupt/bulk).
// ep is the endpoint number (without direction). dir is usbip.DirIn or usbip.DirOut.
// For IN transfers, return the payload to send; for OUT, consume 'out' and return nil.
HandleTransfer(ep uint32, dir uint32, out []byte) []byte
GetDescriptor() *Descriptor
GetDeviceSpecificArgs() map[string]any
}
Device is the minimal interface a device must implement. It only handles non-EP0 (interrupt/bulk) transfers.
type DeviceDescriptor ¶
type DeviceDescriptor struct {
BcdUSB uint16 // LE
BDeviceClass uint8
BDeviceSubClass uint8
BDeviceProtocol uint8
BMaxPacketSize0 uint8
IDVendor uint16 // LE; may get overridden
IDProduct uint16 // LE; may get overridden
BcdDevice uint16 // LE
IManufacturer uint8
IProduct uint8
ISerialNumber uint8
BNumConfigurations uint8
Speed uint32 // USB speed: 1=low, 2=full, 3=high, 4=super
}
DeviceDescriptor represents the standard USB device descriptor. BLength is computed dynamically; BDescriptorType is implied DeviceDescType.
type EndpointDescriptor ¶
type EndpointDescriptor struct {
BEndpointAddress uint8
BMAttributes uint8
WMaxPacketSize uint16 // LE
BInterval uint8
// ClassDescriptors are optional endpoint-level class-specific descriptors
// emitted immediately after this endpoint descriptor.
ClassDescriptors []ClassSpecificDescriptor
}
EndpointDescriptor (7 bytes) for each endpoint.
func (EndpointDescriptor) Write ¶
func (e EndpointDescriptor) Write(b *bytes.Buffer)
type HIDDescriptor ¶
type HIDDescriptor struct {
BcdHID uint16 // LE
BCountryCode uint8
Descriptors []HIDSubDescriptor
}
HIDDescriptor is the HID class descriptor (0x21) for HID-class interfaces.
bDescriptorType is fixed to HIDDescType (0x21). bLength is auto-calculated as: 6 + 3*len(Descriptors).
func (HIDDescriptor) IsZero ¶ added in v0.3.2
func (h HIDDescriptor) IsZero() bool
type HIDFunction ¶ added in v0.3.2
type HIDFunction struct {
Descriptor HIDDescriptor
ReportDescriptor hid.ReportDescriptor
// ReportDescriptorBytes, when non-empty, is returned verbatim as the HID report
// descriptor (0x22) and used for HID report length calculation. This is useful for
// complex, vendor-specific descriptors that are easier to provide as raw bytes.
ReportDescriptorBytes Data
}
HIDFunction bundles the HID class descriptor (0x21) and the report descriptor (0x22) for a HID-class interface.
func (HIDFunction) DescriptorBytes ¶ added in v0.3.2
func (f HIDFunction) DescriptorBytes() (Data, error)
DescriptorBytes returns the HID class descriptor (0x21) bytes.
func (HIDFunction) ReportBytes ¶ added in v0.3.2
func (f HIDFunction) ReportBytes() (Data, error)
ReportBytes returns the HID report descriptor (0x22) bytes.
type HIDSubDescriptor ¶ added in v0.3.2
HIDSubDescriptor is one subordinate descriptor entry in the HID class descriptor.
Type is typically ReportDescType (0x22). If Type==ReportDescType and Length==0, the server will auto-fill Length from the associated HID report descriptor at serialization time.
type InterfaceAssociationDescriptor ¶ added in v0.7.0
type InterfaceAssociationDescriptor struct {
BFirstInterface uint8
BInterfaceCount uint8
BFunctionClass uint8
BFunctionSubClass uint8
BFunctionProtocol uint8
IFunction uint8
}
InterfaceAssociationDescriptor describes a USB Interface Association Descriptor (IAD), used by composite devices to group related interfaces.
func (InterfaceAssociationDescriptor) Write ¶ added in v0.7.0
func (i InterfaceAssociationDescriptor) Write(b *bytes.Buffer)
type InterfaceConfig ¶
type InterfaceConfig struct {
Descriptor InterfaceDescriptor
Endpoints []EndpointDescriptor
// HID describes a HID-class interface (bInterfaceClass=0x03).
// If set, the server will emit the HID descriptor (0x21) in the configuration
// descriptor and serve the report descriptor (0x22) via GET_DESCRIPTOR.
HID *HIDFunction
// ClassDescriptors are additional interface-level class-specific descriptors
// emitted as part of the configuration descriptor (after the interface descriptor
// and before the endpoints).
//
// This is also used for vendor-specific interfaces that need to expose opaque
// descriptors (e.g. type 0x21 blobs on Xbox360).
ClassDescriptors []ClassSpecificDescriptor
}
InterfaceConfig holds all descriptors for a single interface for bus management.
type InterfaceDescriptor ¶
type InterfaceDescriptor struct {
BInterfaceNumber uint8
BAlternateSetting uint8
BNumEndpoints uint8
BInterfaceClass uint8
BInterfaceSubClass uint8
BInterfaceProtocol uint8
IInterface uint8
}
InterfaceDescriptor (9 bytes) for each interface altsetting.
func (InterfaceDescriptor) Write ¶
func (i InterfaceDescriptor) Write(b *bytes.Buffer)
type MicrosoftOS10Descriptor ¶ added in v0.7.0
type MicrosoftOS10Descriptor struct {
VendorCode uint8
InterfaceNumber uint8
CompatibleID string
SubCompatibleID string
DeviceInterfaceGUID string
}
MicrosoftOS10Descriptor enables the Microsoft OS 1.0 descriptor probe used by Windows to bind vendor-specific interfaces to inbox drivers such as WinUSB.
func (MicrosoftOS10Descriptor) CompatibleIDDescriptor ¶ added in v0.7.0
func (d MicrosoftOS10Descriptor) CompatibleIDDescriptor() []byte
func (MicrosoftOS10Descriptor) ControlResponse ¶ added in v0.7.0
func (d MicrosoftOS10Descriptor) ControlResponse(wValue, wIndex uint16) ([]byte, bool)
func (MicrosoftOS10Descriptor) EffectiveVendorCode ¶ added in v0.7.0
func (d MicrosoftOS10Descriptor) EffectiveVendorCode() uint8
func (MicrosoftOS10Descriptor) ExtendedPropertiesDescriptor ¶ added in v0.7.0
func (d MicrosoftOS10Descriptor) ExtendedPropertiesDescriptor() []byte
func (MicrosoftOS10Descriptor) StringDescriptor ¶ added in v0.7.0
func (d MicrosoftOS10Descriptor) StringDescriptor() []byte