machine/usb/descriptor: further refactor HID report creation

Signed-off-by: deadprogram <ron@hybridgroup.com>
Этот коммит содержится в:
deadprogram 2023-04-29 09:31:50 +02:00 коммит произвёл Ron Evans
родитель 90935703e6
коммит 1d5c5ca2ef
3 изменённых файлов: 174 добавлений и 113 удалений

Просмотреть файл

@ -156,7 +156,7 @@ var CDCHID = Descriptor{
HIDUsageMinimum(0), HIDUsageMinimum(0),
HIDUsageMaximum(255), HIDUsageMaximum(255),
HIDInputDataAryAbs, HIDInputDataAryAbs,
HIDEndCollection, HIDCollectionEnd,
HIDUsagePageGenericDesktop, HIDUsagePageGenericDesktop,
HIDUsageDesktopMouse, HIDUsageDesktopMouse,
@ -186,22 +186,20 @@ var CDCHID = Descriptor{
HIDReportSize(8), HIDReportSize(8),
HIDReportCount(3), HIDReportCount(3),
HIDInputDataVarRel, HIDInputDataVarRel,
HIDEndCollection, HIDCollectionEnd,
HIDEndCollection, HIDCollectionEnd,
HIDUsagePageConsumer, HIDUsagePageConsumer,
HIDUsageConsumerControl, HIDUsageConsumerControl,
HIDCollectionApplication, HIDCollectionApplication,
HIDReportID(3), HIDReportID(3),
HIDLogicalMinimum(0), HIDLogicalMinimum(0),
[]byte{ HIDLogicalMaximum(8191),
0x26, 0xFF, 0x1F, // Logical Maximum (8191) HIDUsageMinimum(0),
0x19, 0x00, // Usage Minimum (Unassigned) HIDUsageMaximum(0x1FFF),
0x2A, 0xFF, 0x1F, // Usage Maximum (0x1FFF)
},
HIDReportSize(16), HIDReportSize(16),
HIDReportCount(1), HIDReportCount(1),
HIDInputDataAryAbs, HIDInputDataAryAbs,
HIDEndCollection}), HIDCollectionEnd}),
}, },
} }

Просмотреть файл

@ -1,130 +1,201 @@
package descriptor package descriptor
var ( import (
HIDUsagePageGenericDesktop = []byte{0x05, 0x01} "encoding/binary"
HIDUsagePageSimulationControls = []byte{0x05, 0x02} )
HIDUsagePageVRControls = []byte{0x05, 0x03}
HIDUsagePageSportControls = []byte{0x05, 0x04} const (
HIDUsagePageGameControls = []byte{0x05, 0x05} hidUsagePage = 0x05
HIDUsagePageGenericControls = []byte{0x05, 0x06} hidUsage = 0x09
HIDUsagePageKeyboard = []byte{0x05, 0x07} hidLogicalMinimum = 0x15
HIDUsagePageLED = []byte{0x05, 0x08} hidLogicalMaximum = 0x25
HIDUsagePageButton = []byte{0x05, 0x09} hidUsageMinimum = 0x19
HIDUsagePageOrdinal = []byte{0x05, 0x0A} hidUsageMaximum = 0x29
HIDUsagePageTelephony = []byte{0x05, 0x0B} hidPhysicalMinimum = 0x35
HIDUsagePageConsumer = []byte{0x05, 0x0C} hidPhysicalMaximum = 0x46
HIDUsagePageDigitizers = []byte{0x05, 0x0D} hidUnitExponent = 0x55
HIDUsagePageHaptics = []byte{0x05, 0x0E} hidUnit = 0x65
HIDUsagePagePhysicalInput = []byte{0x05, 0x0F} hidCollection = 0xa1
HIDUsagePageUnicode = []byte{0x05, 0x10} hidInput = 0x81
HIDUsagePageSoC = []byte{0x05, 0x11} hidReportSize = 0x75
HIDUsagePageEyeHeadTrackers = []byte{0x05, 0x12} hidReportCount = 0x95
HIDUsagePageAuxDisplay = []byte{0x05, 0x14} hidReportID = 0x85
HIDUsagePageSensors = []byte{0x05, 0x20}
HIDUsagePageMedicalInstrument = []byte{0x05, 0x40}
HIDUsagePageBrailleDisplay = []byte{0x05, 0x41}
HIDUsagePageLighting = []byte{0x05, 0x59}
HIDUsagePageMonitor = []byte{0x05, 0x80}
HIDUsagePageMonitorEnum = []byte{0x05, 0x81}
HIDUsagePageVESA = []byte{0x05, 0x82}
HIDUsagePagePower = []byte{0x05, 0x84}
HIDUsagePageBatterySystem = []byte{0x05, 0x85}
HIDUsagePageBarcodeScanner = []byte{0x05, 0x8C}
HIDUsagePageScales = []byte{0x05, 0x8D}
HIDUsagePageMagneticStripe = []byte{0x05, 0x8E}
HIDUsagePageCameraControl = []byte{0x05, 0x90}
HIDUsagePageArcade = []byte{0x05, 0x91}
HIDUsagePageGaming = []byte{0x05, 0x92}
) )
var ( var (
HIDUsageDesktopPointer = []byte{0x09, 0x01} HIDUsagePageGenericDesktop = []byte{hidUsagePage, 0x01}
HIDUsageDesktopMouse = []byte{0x09, 0x02} HIDUsagePageSimulationControls = []byte{hidUsagePage, 0x02}
HIDUsageDesktopJoystick = []byte{0x09, 0x04} HIDUsagePageVRControls = []byte{hidUsagePage, 0x03}
HIDUsageDesktopGamepad = []byte{0x09, 0x05} HIDUsagePageSportControls = []byte{hidUsagePage, 0x04}
HIDUsageDesktopKeyboard = []byte{0x09, 0x06} HIDUsagePageGameControls = []byte{hidUsagePage, 0x05}
HIDUsageDesktopKeypad = []byte{0x09, 0x07} HIDUsagePageGenericControls = []byte{hidUsagePage, 0x06}
HIDUsageDesktopMultiaxis = []byte{0x09, 0x08} HIDUsagePageKeyboard = []byte{hidUsagePage, 0x07}
HIDUsageDesktopTablet = []byte{0x09, 0x09} HIDUsagePageLED = []byte{hidUsagePage, 0x08}
HIDUsageDesktopWaterCooling = []byte{0x09, 0x0A} HIDUsagePageButton = []byte{hidUsagePage, 0x09}
HIDUsageDesktopChassis = []byte{0x09, 0x0B} HIDUsagePageOrdinal = []byte{hidUsagePage, 0x0A}
HIDUsageDesktopWireless = []byte{0x09, 0x0C} HIDUsagePageTelephony = []byte{hidUsagePage, 0x0B}
HIDUsageDesktopPortable = []byte{0x09, 0x0D} HIDUsagePageConsumer = []byte{hidUsagePage, 0x0C}
HIDUsageDesktopSystemMultiaxis = []byte{0x09, 0x0E} HIDUsagePageDigitizers = []byte{hidUsagePage, 0x0D}
HIDUsageDesktopSpatial = []byte{0x09, 0x0F} HIDUsagePageHaptics = []byte{hidUsagePage, 0x0E}
HIDUsageDesktopAssistive = []byte{0x09, 0x10} HIDUsagePagePhysicalInput = []byte{hidUsagePage, 0x0F}
HIDUsageDesktopDock = []byte{0x09, 0x11} HIDUsagePageUnicode = []byte{hidUsagePage, 0x10}
HIDUsageDesktopDockable = []byte{0x09, 0x12} HIDUsagePageSoC = []byte{hidUsagePage, 0x11}
HIDUsageDesktopCallState = []byte{0x09, 0x13} HIDUsagePageEyeHeadTrackers = []byte{hidUsagePage, 0x12}
HIDUsageDesktopX = []byte{0x09, 0x30} HIDUsagePageAuxDisplay = []byte{hidUsagePage, 0x14}
HIDUsageDesktopY = []byte{0x09, 0x31} HIDUsagePageSensors = []byte{hidUsagePage, 0x20}
HIDUsageDesktopZ = []byte{0x09, 0x32} HIDUsagePageMedicalInstrument = []byte{hidUsagePage, 0x40}
HIDUsageDesktopRx = []byte{0x09, 0x33} HIDUsagePageBrailleDisplay = []byte{hidUsagePage, 0x41}
HIDUsageDesktopRy = []byte{0x09, 0x34} HIDUsagePageLighting = []byte{hidUsagePage, 0x59}
HIDUsageDesktopRz = []byte{0x09, 0x35} HIDUsagePageMonitor = []byte{hidUsagePage, 0x80}
HIDUsageDesktopSlider = []byte{0x09, 0x36} HIDUsagePageMonitorEnum = []byte{hidUsagePage, 0x81}
HIDUsageDesktopDial = []byte{0x09, 0x37} HIDUsagePageVESA = []byte{hidUsagePage, 0x82}
HIDUsageDesktopWheel = []byte{0x09, 0x38} HIDUsagePagePower = []byte{hidUsagePage, 0x84}
HIDUsageDesktopHatSwitch = []byte{0x09, 0x39} HIDUsagePageBatterySystem = []byte{hidUsagePage, 0x85}
HIDUsageDesktopCountedBuffer = []byte{0x09, 0x3A} 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 ( var (
HIDUsageConsumerControl = []byte{0x09, 0x01} HIDUsageDesktopPointer = []byte{hidUsage, 0x01}
HIDUsageConsumerNumericKeypad = []byte{0x09, 0x02} HIDUsageDesktopMouse = []byte{hidUsage, 0x02}
HIDUsageConsumerProgrammableButtons = []byte{0x09, 0x03} HIDUsageDesktopJoystick = []byte{hidUsage, 0x04}
HIDUsageConsumerMicrophone = []byte{0x09, 0x04} HIDUsageDesktopGamepad = []byte{hidUsage, 0x05}
HIDUsageConsumerHeadphone = []byte{0x09, 0x05} HIDUsageDesktopKeyboard = []byte{hidUsage, 0x06}
HIDUsageConsumerGraphicEqualizer = []byte{0x09, 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 ( var (
HIDCollectionPhysical = []byte{0xa1, 0x00} HIDUsageConsumerControl = []byte{hidUsage, 0x01}
HIDCollectionApplication = []byte{0xa1, 0x01} HIDUsageConsumerNumericKeypad = []byte{hidUsage, 0x02}
HIDUsageConsumerProgrammableButtons = []byte{hidUsage, 0x03}
HIDUsageConsumerMicrophone = []byte{hidUsage, 0x04}
HIDUsageConsumerHeadphone = []byte{hidUsage, 0x05}
HIDUsageConsumerGraphicEqualizer = []byte{hidUsage, 0x06}
) )
var ( var (
HIDEndCollection = []byte{0xc0} HIDCollectionPhysical = []byte{hidCollection, 0x00}
HIDCollectionApplication = []byte{hidCollection, 0x01}
HIDCollectionEnd = []byte{0xc0}
) )
var ( var (
// Input (Data,Ary,Abs), Key arrays (6 bytes) // Input (Data,Ary,Abs), Key arrays (6 bytes)
HIDInputDataAryAbs = []byte{0x81, 0x00} HIDInputDataAryAbs = []byte{hidInput, 0x00}
// Input (Data, Variable, Absolute), Modifier byte // Input (Data, Variable, Absolute), Modifier byte
HIDInputDataVarAbs = []byte{0x81, 0x02} HIDInputDataVarAbs = []byte{hidInput, 0x02}
// Input (Const,Var,Abs), Modifier byte // Input (Const,Var,Abs), Modifier byte
HIDInputConstVarAbs = []byte{0x81, 0x03} HIDInputConstVarAbs = []byte{hidInput, 0x03}
// Input (Data, Variable, Relative), 2 position bytes (X & Y) // Input (Data, Variable, Relative), 2 position bytes (X & Y)
HIDInputDataVarRel = []byte{0x81, 0x06} HIDInputDataVarRel = []byte{hidInput, 0x06}
) )
func HIDReportSize(size int) []byte { func HIDReportSize(size int) []byte {
return []byte{0x75, byte(size)} return []byte{hidReportSize, byte(size)}
} }
func HIDReportCount(count int) []byte { func HIDReportCount(count int) []byte {
return []byte{0x95, byte(count)} return []byte{hidReportCount, byte(count)}
} }
func HIDReportID(id int) []byte { func HIDReportID(id int) []byte {
return []byte{0x85, byte(id)} return []byte{hidReportID, byte(id)}
} }
func HIDLogicalMinimum(min int) []byte { func HIDLogicalMinimum(min int) []byte {
return []byte{0x15, byte(min)} 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 { func HIDLogicalMaximum(max int) []byte {
return []byte{0x25, byte(max)} 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 { func HIDUsageMinimum(min int) []byte {
return []byte{0x19, byte(min)} 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 { func HIDUsageMaximum(max int) []byte {
return []byte{0x29, byte(max)} 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)}
} }

Просмотреть файл

@ -83,40 +83,32 @@ var JoystickDefaultHIDReport = Append([][]byte{
HIDInputDataVarAbs, HIDInputDataVarAbs,
HIDReportCount(1), HIDReportCount(1),
HIDReportSize(3), HIDReportSize(3),
[]byte{ HIDUnitExponent(-16),
0x55, 0x00, // Unit Exponent (-16) HIDUnit(0),
0x65, 0x00, // Unit (0x00)
},
HIDInputDataVarAbs, HIDInputDataVarAbs,
HIDUsagePageGenericDesktop, HIDUsagePageGenericDesktop,
HIDUsageDesktopHatSwitch, HIDUsageDesktopHatSwitch,
HIDLogicalMinimum(0), HIDLogicalMinimum(0),
HIDLogicalMaximum(7), HIDLogicalMaximum(7),
[]byte{ HIDPhysicalMinimum(0),
0x35, 0x00, // PHYSICAL_MINIMUM (0) HIDPhysicalMaximum(315),
0x46, 0x3b, 0x01, // PHYSICAL_MAXIMUM(315) HIDUnit(0x14),
0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
},
HIDReportCount(1), HIDReportCount(1),
HIDReportSize(4), HIDReportSize(4),
HIDInputDataVarAbs, HIDInputDataVarAbs,
HIDUsageDesktopHatSwitch, HIDUsageDesktopHatSwitch,
HIDLogicalMinimum(0), HIDLogicalMinimum(0),
HIDLogicalMaximum(7), HIDLogicalMaximum(7),
[]byte{ HIDPhysicalMinimum(0),
0x35, 0x00, // PHYSICAL_MINIMUM (0) HIDPhysicalMaximum(315),
0x46, 0x3b, 0x01, // PHYSICAL_MAXIMUM(315) HIDUnit(0x14),
0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
},
HIDReportCount(1), HIDReportCount(1),
HIDReportSize(4), HIDReportSize(4),
HIDInputDataVarAbs, HIDInputDataVarAbs,
HIDUsageDesktopPointer, HIDUsageDesktopPointer,
[]byte{ HIDLogicalMinimum(-32767),
0x16, 0x01, 0x80, // LOGICAL_MINIMUM (-32767) HIDLogicalMaximum(32767),
0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767)
},
HIDReportCount(6), HIDReportCount(6),
HIDReportSize(16), HIDReportSize(16),
HIDCollectionPhysical, HIDCollectionPhysical,
@ -127,8 +119,8 @@ var JoystickDefaultHIDReport = Append([][]byte{
HIDUsageDesktopRy, HIDUsageDesktopRy,
HIDUsageDesktopRz, HIDUsageDesktopRz,
HIDInputDataVarAbs, HIDInputDataVarAbs,
HIDEndCollection, HIDCollectionEnd,
HIDEndCollection, HIDCollectionEnd,
}) })
// CDCJoystick requires that you append the JoystickDescriptor // CDCJoystick requires that you append the JoystickDescriptor