usb

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package usb contains helpers for building USB descriptors and data.

Index

Constants

View Source
const (
	DeviceDescType    = 0x01
	ConfigDescType    = 0x02
	InterfaceDescType = 0x04
	EndpointDescType  = 0x05
	HIDDescType       = 0x21
	ReportDescType    = 0x22
)

USB descriptor type constants

View Source
const (
	DeviceDescLen    = 18
	ConfigDescLen    = 9
	InterfaceDescLen = 9
	EndpointDescLen  = 7
	HIDDescLen       = 9
)

Descriptor lengths in bytes (fixed values from USB spec)

Variables

This section is empty.

Functions

func EncodeStringDescriptor

func EncodeStringDescriptor(s string) []byte

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 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 Descriptor

type Descriptor struct {
	Device     DeviceDescriptor
	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.

type Device

type Device interface {
	// HandleTransfer processes a non-EP0 transfer (interrupt/bulk).
	// ep is the endpoint number (without direction). dir is protocol.DirIn or protocol.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
}

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
}

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
	BNumDescriptors   uint8
	ClassDescType     uint8  // 0x22 (report)
	WDescriptorLength uint16 // LE, report descriptor length
}

HIDDescriptor (class descriptor, 0x21) with one subordinate report descriptor (0x22).

func (HIDDescriptor) Write

func (h HIDDescriptor) Write(b *bytes.Buffer)

type InterfaceConfig

type InterfaceConfig struct {
	Descriptor    InterfaceDescriptor
	Endpoints     []EndpointDescriptor
	HIDDescriptor []byte // optional HID class descriptor (0x21)
	HIDReport     []byte // optional HID report descriptor (0x22)
	VendorData    []byte // optional vendor-specific bytes
}

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 ReportDescriptor

type ReportDescriptor struct {
	Data []byte
}

ReportDescriptor is a container for HID report descriptor bytes (0x22). Builders can populate Data to emit via Bytes().

func (ReportDescriptor) Bytes

func (r ReportDescriptor) Bytes() []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL