aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorUnrud <[email protected]>2024-08-05 19:23:13 +0200
committerRon Evans <[email protected]>2024-08-14 18:43:27 +0200
commitf1516ad3eefd79b951b52e33637ee67bfd80efff (patch)
tree90570d8c5194beb3f5c6ec0feeec07fbd2667115 /src
parent4dc07d6cc3f06479faca543f94b286a047197b7e (diff)
downloadtinygo-f1516ad3eefd79b951b52e33637ee67bfd80efff.tar.gz
tinygo-f1516ad3eefd79b951b52e33637ee67bfd80efff.zip
machine/usb/descriptor: Fix encoding of values
The limit for positive values is incorrect and leads to an overflow (e.g. 0xFF and 0xFFFF become -1)
Diffstat (limited to 'src')
-rw-r--r--src/machine/usb/descriptor/hid.go7
-rw-r--r--src/machine/usb/descriptor/hidreport.go78
2 files changed, 34 insertions, 51 deletions
diff --git a/src/machine/usb/descriptor/hid.go b/src/machine/usb/descriptor/hid.go
index d8f86a41e..ea65f1ed9 100644
--- a/src/machine/usb/descriptor/hid.go
+++ b/src/machine/usb/descriptor/hid.go
@@ -103,7 +103,7 @@ var classHID = [ClassHIDTypeLen]byte{
0x00, // CountryCode
0x01, // NumDescriptors
0x22, // ClassType
- 0x90, // ClassLength L
+ 0x91, // ClassLength L
0x00, // ClassLength H
}
@@ -131,7 +131,7 @@ var CDCHID = Descriptor{
EndpointEP5OUT.Bytes(),
}),
HID: map[uint16][]byte{
- 2: Append([][]byte{
+ 2: Append([][]byte{ // Update ClassLength in classHID whenever the array length is modified!
HIDUsagePageGenericDesktop,
HIDUsageDesktopKeyboard,
HIDCollectionApplication,
@@ -210,6 +210,7 @@ var CDCHID = Descriptor{
HIDReportSize(16),
HIDReportCount(1),
HIDInputDataAryAbs,
- HIDCollectionEnd}),
+ HIDCollectionEnd,
+ }),
},
}
diff --git a/src/machine/usb/descriptor/hidreport.go b/src/machine/usb/descriptor/hidreport.go
index 5819f6ad6..c6d46eec5 100644
--- a/src/machine/usb/descriptor/hidreport.go
+++ b/src/machine/usb/descriptor/hidreport.go
@@ -1,5 +1,7 @@
package descriptor
+import "math"
+
const (
hidUsagePage = 0x05
hidUsage = 0x09
@@ -130,6 +132,28 @@ var (
HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
)
+func hidShortItem(tag byte, value uint32) []byte {
+ switch {
+ case value <= math.MaxUint8:
+ return []byte{tag | hidSizeValue1, byte(value)}
+ case value <= math.MaxUint16:
+ return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
+ default:
+ return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
+ }
+}
+
+func hidShortItemSigned(tag byte, value int32) []byte {
+ switch {
+ case math.MinInt8 <= value && value <= math.MaxInt8:
+ return []byte{tag | hidSizeValue1, byte(value)}
+ case math.MinInt16 <= value && value <= math.MaxInt16:
+ return []byte{tag | hidSizeValue2, byte(value), byte(value >> 8)}
+ default:
+ return []byte{tag | hidSizeValue4, byte(value), byte(value >> 8), byte(value >> 16), byte(value >> 24)}
+ }
+}
+
func HIDReportSize(size int) []byte {
return []byte{hidReportSize, byte(size)}
}
@@ -143,69 +167,27 @@ func HIDReportID(id int) []byte {
}
func HIDLogicalMinimum(min int) []byte {
- switch {
- case min < -32767 || 65535 < min:
- return []byte{hidLogicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
- case min < -127 || 255 < min:
- return []byte{hidLogicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
- default:
- return []byte{hidLogicalMinimum + hidSizeValue1, byte(min)}
- }
+ return hidShortItemSigned(hidLogicalMinimum, int32(min))
}
func HIDLogicalMaximum(max int) []byte {
- switch {
- case max < -32767 || 65535 < max:
- return []byte{hidLogicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
- case max < -127 || 255 < max:
- return []byte{hidLogicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
- default:
- return []byte{hidLogicalMaximum + hidSizeValue1, byte(max)}
- }
+ return hidShortItemSigned(hidLogicalMaximum, int32(max))
}
func HIDUsageMinimum(min int) []byte {
- switch {
- case min < -32767 || 65535 < min:
- return []byte{hidUsageMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
- case min < -127 || 255 < min:
- return []byte{hidUsageMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
- default:
- return []byte{hidUsageMinimum + hidSizeValue1, byte(min)}
- }
+ return hidShortItem(hidUsageMinimum, uint32(min))
}
func HIDUsageMaximum(max int) []byte {
- switch {
- case max < -32767 || 65535 < max:
- return []byte{hidUsageMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
- case max < -127 || 255 < max:
- return []byte{hidUsageMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
- default:
- return []byte{hidUsageMaximum + hidSizeValue1, byte(max)}
- }
+ return hidShortItem(hidUsageMaximum, uint32(max))
}
func HIDPhysicalMinimum(min int) []byte {
- switch {
- case min < -32767 || 65535 < min:
- return []byte{hidPhysicalMinimum + hidSizeValue4, uint8(min), uint8(min >> 8), uint8(min >> 16), uint8(min >> 24)}
- case min < -127 || 255 < min:
- return []byte{hidPhysicalMinimum + hidSizeValue2, uint8(min), uint8(min >> 8)}
- default:
- return []byte{hidPhysicalMinimum + hidSizeValue1, byte(min)}
- }
+ return hidShortItemSigned(hidPhysicalMinimum, int32(min))
}
func HIDPhysicalMaximum(max int) []byte {
- switch {
- case max < -32767 || 65535 < max:
- return []byte{hidPhysicalMaximum + hidSizeValue4, uint8(max), uint8(max >> 8), uint8(max >> 16), uint8(max >> 24)}
- case max < -127 || 255 < max:
- return []byte{hidPhysicalMaximum + hidSizeValue2, uint8(max), uint8(max >> 8)}
- default:
- return []byte{hidPhysicalMaximum + hidSizeValue1, byte(max)}
- }
+ return hidShortItemSigned(hidPhysicalMaximum, int32(max))
}
func HIDUnitExponent(exp int) []byte {