Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/machine/usb/descriptor/cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var InterfaceCDCData = InterfaceType{

var CDC = Descriptor{
Device: DeviceCDC.Bytes(),
Configuration: appendSlices([][]byte{
Configuration: Append([][]byte{
ConfigurationCDC.Bytes(),
InterfaceAssociationCDC.Bytes(),
InterfaceCDCControl.Bytes(),
Expand Down
2 changes: 1 addition & 1 deletion src/machine/usb/descriptor/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d *Descriptor) Configure(idVendor, idProduct uint16) {
conf.TotalLength(uint16(len(d.Configuration)))
}

func appendSlices[T any](slices [][]T) []T {
func Append[T any](slices [][]T) []T {
var size, pos int

for _, s := range slices {
Expand Down
141 changes: 72 additions & 69 deletions src/machine/usb/descriptor/hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var ClassHID = ClassHIDType{

var CDCHID = Descriptor{
Device: DeviceCDC.Bytes(),
Configuration: appendSlices([][]byte{
Configuration: Append([][]byte{
ConfigurationCDCHID.Bytes(),
InterfaceAssociationCDC.Bytes(),
InterfaceCDCControl.Bytes(),
Expand All @@ -130,73 +130,76 @@ var CDCHID = Descriptor{
EndpointEP4IN.Bytes(),
}),
HID: map[uint16][]byte{
2: []byte{
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x06, // Usage (Keyboard)
0xa1, 0x01, // Collection (Application)
0x85, 0x02, // Report ID (2)
0x05, 0x07, // Usage Page (KeyCodes)
0x19, 0xe0, // Usage Minimum (224)
0x29, 0xe7, // Usage Maximum (231)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x08, // Report Count (8)
0x81, 0x02, // Input (Data, Variable, Absolute), Modifier byte
0x95, 0x01, // Report Count (1)
0x75, 0x08, // Report Size (8)
0x81, 0x03, //
0x95, 0x06, // Report Count (6)
0x75, 0x08, // Report Size (8)
0x15, 0x00, // Logical Minimum (0),
0x25, 0xFF, //
0x05, 0x07, // Usage Page (KeyCodes)
0x19, 0x00, // Usage Minimum (0)
0x29, 0xFF, // Usage Maximum (255)
0x81, 0x00, // Input (Data, Array), Key arrays (6 bytes)
0xc0, // End Collection

0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse)
0xa1, 0x01, // Collection (Application)
0x09, 0x01, // Usage (Pointer)
0xa1, 0x00, // Collection (Physical)
0x85, 0x01, // Report ID (1)
0x05, 0x09, // Usage Page (Buttons)
0x19, 0x01, // Usage Minimum (01)
0x29, 0x05, // Usage Maximun (05)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
0x95, 0x01, // Report Count (1)
0x75, 0x03, // Report Size (3)
0x81, 0x03, //
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x09, 0x38, //
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7f, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x03, // Report Count (3)
0x81, 0x06, // Input (Data, Variable, Relative), 2 position bytes (X & Y)
0xc0, // End Collection
0xc0, // End Collection

0x05, 0x0C, // Usage Page (Consumer)
0x09, 0x01, // Usage (Consumer Control)
0xA1, 0x01, // Collection (Application)
0x85, 0x03, // Report ID (3)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x1F, // Logical Maximum (8191)
0x19, 0x00, // Usage Minimum (Unassigned)
0x2A, 0xFF, 0x1F, // Usage Maximum (0x1FFF)
0x75, 0x10, // Report Size (16)
0x95, 0x01, // Report Count (1)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
},
2: Append([][]byte{
HIDUsagePageGenericDesktop,
HIDUsageDesktopKeyboard,
HIDCollectionApplication,
HIDReportID(2),

HIDUsagePageKeyboard,
HIDUsageMinimum(224),
HIDUsageMaximum(231),
HIDLogicalMinimum(0),
HIDLogicalMaximum(1),
HIDReportSize(1),
HIDReportCount(8),
HIDInputDataVarAbs,
HIDReportCount(1),
HIDReportSize(8),
HIDInputConstVarAbs,
HIDReportCount(6),
HIDReportSize(8),
HIDLogicalMinimum(0),
HIDLogicalMaximum(255),

HIDUsagePageKeyboard,
HIDUsageMinimum(0),
HIDUsageMaximum(255),
HIDInputDataAryAbs,
HIDCollectionEnd,

HIDUsagePageGenericDesktop,
HIDUsageDesktopMouse,
HIDCollectionApplication,
HIDUsageDesktopPointer,
HIDCollectionPhysical,
HIDReportID(1),

HIDUsagePageButton,
HIDUsageMinimum(1),
HIDUsageMaximum(5),
HIDLogicalMinimum(0),
HIDLogicalMaximum(1),
HIDReportCount(5),
HIDReportSize(1),
HIDInputDataVarAbs,
HIDReportCount(1),
HIDReportSize(3),
HIDInputConstVarAbs,

HIDUsagePageGenericDesktop,
HIDUsageDesktopX,
HIDUsageDesktopY,
HIDUsageDesktopWheel,
HIDLogicalMinimum(-127),
HIDLogicalMaximum(127),
HIDReportSize(8),
HIDReportCount(3),
HIDInputDataVarRel,
HIDCollectionEnd,
HIDCollectionEnd,

HIDUsagePageConsumer,
HIDUsageConsumerControl,
HIDCollectionApplication,
HIDReportID(3),
HIDLogicalMinimum(0),
HIDLogicalMaximum(8191),
HIDUsageMinimum(0),
HIDUsageMaximum(0x1FFF),
HIDReportSize(16),
HIDReportCount(1),
HIDInputDataAryAbs,
HIDCollectionEnd}),
},
}
201 changes: 201 additions & 0 deletions src/machine/usb/descriptor/hidreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package descriptor

import (
"encoding/binary"
)

const (
hidUsagePage = 0x05
hidUsage = 0x09
hidLogicalMinimum = 0x15
hidLogicalMaximum = 0x25
hidUsageMinimum = 0x19
hidUsageMaximum = 0x29
hidPhysicalMinimum = 0x35
hidPhysicalMaximum = 0x46
hidUnitExponent = 0x55
hidUnit = 0x65
hidCollection = 0xa1
hidInput = 0x81
hidReportSize = 0x75
hidReportCount = 0x95
hidReportID = 0x85
)

var (
HIDUsagePageGenericDesktop = []byte{hidUsagePage, 0x01}
HIDUsagePageSimulationControls = []byte{hidUsagePage, 0x02}
HIDUsagePageVRControls = []byte{hidUsagePage, 0x03}
HIDUsagePageSportControls = []byte{hidUsagePage, 0x04}
HIDUsagePageGameControls = []byte{hidUsagePage, 0x05}
HIDUsagePageGenericControls = []byte{hidUsagePage, 0x06}
HIDUsagePageKeyboard = []byte{hidUsagePage, 0x07}
HIDUsagePageLED = []byte{hidUsagePage, 0x08}
HIDUsagePageButton = []byte{hidUsagePage, 0x09}
HIDUsagePageOrdinal = []byte{hidUsagePage, 0x0A}
HIDUsagePageTelephony = []byte{hidUsagePage, 0x0B}
HIDUsagePageConsumer = []byte{hidUsagePage, 0x0C}
HIDUsagePageDigitizers = []byte{hidUsagePage, 0x0D}
HIDUsagePageHaptics = []byte{hidUsagePage, 0x0E}
HIDUsagePagePhysicalInput = []byte{hidUsagePage, 0x0F}
HIDUsagePageUnicode = []byte{hidUsagePage, 0x10}
HIDUsagePageSoC = []byte{hidUsagePage, 0x11}
HIDUsagePageEyeHeadTrackers = []byte{hidUsagePage, 0x12}
HIDUsagePageAuxDisplay = []byte{hidUsagePage, 0x14}
HIDUsagePageSensors = []byte{hidUsagePage, 0x20}
HIDUsagePageMedicalInstrument = []byte{hidUsagePage, 0x40}
HIDUsagePageBrailleDisplay = []byte{hidUsagePage, 0x41}
HIDUsagePageLighting = []byte{hidUsagePage, 0x59}
HIDUsagePageMonitor = []byte{hidUsagePage, 0x80}
HIDUsagePageMonitorEnum = []byte{hidUsagePage, 0x81}
HIDUsagePageVESA = []byte{hidUsagePage, 0x82}
HIDUsagePagePower = []byte{hidUsagePage, 0x84}
HIDUsagePageBatterySystem = []byte{hidUsagePage, 0x85}
HIDUsagePageBarcodeScanner = []byte{hidUsagePage, 0x8C}
HIDUsagePageScales = []byte{hidUsagePage, 0x8D}
HIDUsagePageMagneticStripe = []byte{hidUsagePage, 0x8E}
HIDUsagePageCameraControl = []byte{hidUsagePage, 0x90}
HIDUsagePageArcade = []byte{hidUsagePage, 0x91}
HIDUsagePageGaming = []byte{hidUsagePage, 0x92}
)

var (
HIDUsageDesktopPointer = []byte{hidUsage, 0x01}
HIDUsageDesktopMouse = []byte{hidUsage, 0x02}
HIDUsageDesktopJoystick = []byte{hidUsage, 0x04}
HIDUsageDesktopGamepad = []byte{hidUsage, 0x05}
HIDUsageDesktopKeyboard = []byte{hidUsage, 0x06}
HIDUsageDesktopKeypad = []byte{hidUsage, 0x07}
HIDUsageDesktopMultiaxis = []byte{hidUsage, 0x08}
HIDUsageDesktopTablet = []byte{hidUsage, 0x09}
HIDUsageDesktopWaterCooling = []byte{hidUsage, 0x0A}
HIDUsageDesktopChassis = []byte{hidUsage, 0x0B}
HIDUsageDesktopWireless = []byte{hidUsage, 0x0C}
HIDUsageDesktopPortable = []byte{hidUsage, 0x0D}
HIDUsageDesktopSystemMultiaxis = []byte{hidUsage, 0x0E}
HIDUsageDesktopSpatial = []byte{hidUsage, 0x0F}
HIDUsageDesktopAssistive = []byte{hidUsage, 0x10}
HIDUsageDesktopDock = []byte{hidUsage, 0x11}
HIDUsageDesktopDockable = []byte{hidUsage, 0x12}
HIDUsageDesktopCallState = []byte{hidUsage, 0x13}
HIDUsageDesktopX = []byte{hidUsage, 0x30}
HIDUsageDesktopY = []byte{hidUsage, 0x31}
HIDUsageDesktopZ = []byte{hidUsage, 0x32}
HIDUsageDesktopRx = []byte{hidUsage, 0x33}
HIDUsageDesktopRy = []byte{hidUsage, 0x34}
HIDUsageDesktopRz = []byte{hidUsage, 0x35}
HIDUsageDesktopSlider = []byte{hidUsage, 0x36}
HIDUsageDesktopDial = []byte{hidUsage, 0x37}
HIDUsageDesktopWheel = []byte{hidUsage, 0x38}
HIDUsageDesktopHatSwitch = []byte{hidUsage, 0x39}
HIDUsageDesktopCountedBuffer = []byte{hidUsage, 0x3A}
)

var (
HIDUsageConsumerControl = []byte{hidUsage, 0x01}
HIDUsageConsumerNumericKeypad = []byte{hidUsage, 0x02}
HIDUsageConsumerProgrammableButtons = []byte{hidUsage, 0x03}
HIDUsageConsumerMicrophone = []byte{hidUsage, 0x04}
HIDUsageConsumerHeadphone = []byte{hidUsage, 0x05}
HIDUsageConsumerGraphicEqualizer = []byte{hidUsage, 0x06}
)

var (
HIDCollectionPhysical = []byte{hidCollection, 0x00}
HIDCollectionApplication = []byte{hidCollection, 0x01}
HIDCollectionEnd = []byte{0xc0}
)

var (
// Input (Data,Ary,Abs), Key arrays (6 bytes)
HIDInputDataAryAbs = []byte{hidInput, 0x00}

// Input (Data, Variable, Absolute), Modifier byte
HIDInputDataVarAbs = []byte{hidInput, 0x02}

// Input (Const,Var,Abs), Modifier byte
HIDInputConstVarAbs = []byte{hidInput, 0x03}

// Input (Data, Variable, Relative), 2 position bytes (X & Y)
HIDInputDataVarRel = []byte{hidInput, 0x06}
)

func HIDReportSize(size int) []byte {
return []byte{hidReportSize, byte(size)}
}

func HIDReportCount(count int) []byte {
return []byte{hidReportCount, byte(count)}
}

func HIDReportID(id int) []byte {
return []byte{hidReportID, byte(id)}
}

func HIDLogicalMinimum(min int) []byte {
if min < -255 {
result := []byte{hidLogicalMinimum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
return result
}

return []byte{hidLogicalMinimum, byte(min)}
}

func HIDLogicalMaximum(max int) []byte {
if max > 255 {
result := []byte{hidLogicalMaximum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
return result
}

return []byte{hidLogicalMaximum, byte(max)}
}

func HIDUsageMinimum(min int) []byte {
if min < -255 {
result := []byte{hidUsageMinimum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
return result
}

return []byte{hidUsageMinimum, byte(min)}
}

func HIDUsageMaximum(max int) []byte {
if max > 255 {
result := []byte{hidUsageMaximum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
return result
}

return []byte{hidUsageMaximum, byte(max)}
}

func HIDPhysicalMinimum(min int) []byte {
if min < -255 {
result := []byte{hidPhysicalMinimum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(min))
return result
}

return []byte{hidPhysicalMinimum, byte(min)}
}

func HIDPhysicalMaximum(max int) []byte {
if max > 255 {
result := []byte{hidPhysicalMaximum, 0x0, 0x0}
binary.LittleEndian.PutUint16(result[1:3], uint16(max))
return result
}

return []byte{hidPhysicalMaximum, byte(max)}
}

func HIDUnitExponent(exp int) []byte {
return []byte{hidUnitExponent, byte(exp)}
}

func HIDUnit(unit int) []byte {
return []byte{hidUnit, byte(unit)}
}
Loading