Skip to content

Commit b661882

Browse files
committed
machine/usbcdc: remove remaining heap allocations for USB CDC implementations
Signed-off-by: deadprogram <[email protected]>
1 parent 321488d commit b661882

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

src/machine/machine_atsamd21.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ func handleStandardSetup(setup usbSetup) bool {
22982298
func cdcSetup(setup usbSetup) bool {
22992299
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
23002300
if setup.bRequest == usb_CDC_GET_LINE_CODING {
2301-
b := make([]byte, 7)
2301+
var b [cdcLineInfoSize]byte
23022302
b[0] = byte(usbLineInfo.dwDTERate)
23032303
b[1] = byte(usbLineInfo.dwDTERate >> 8)
23042304
b[2] = byte(usbLineInfo.dwDTERate >> 16)
@@ -2307,14 +2307,18 @@ func cdcSetup(setup usbSetup) bool {
23072307
b[5] = byte(usbLineInfo.bParityType)
23082308
b[6] = byte(usbLineInfo.bDataBits)
23092309

2310-
sendUSBPacket(0, b)
2310+
sendUSBPacket(0, b[:])
23112311
return true
23122312
}
23132313
}
23142314

23152315
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
23162316
if setup.bRequest == usb_CDC_SET_LINE_CODING {
2317-
b := receiveUSBControlPacket()
2317+
b, err := receiveUSBControlPacket()
2318+
if err != nil {
2319+
return false
2320+
}
2321+
23182322
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
23192323
usbLineInfo.bCharFormat = b[4]
23202324
usbLineInfo.bParityType = b[5]
@@ -2361,7 +2365,9 @@ func sendUSBPacket(ep uint32, data []byte) {
23612365
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos))
23622366
}
23632367

2364-
func receiveUSBControlPacket() []byte {
2368+
func receiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
2369+
var b [cdcLineInfoSize]byte
2370+
23652371
// address
23662372
usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0]))))
23672373

@@ -2376,7 +2382,7 @@ func receiveUSBControlPacket() []byte {
23762382
for (getEPSTATUS(0) & sam.USB_DEVICE_EPSTATUS_BK0RDY) == 0 {
23772383
timeout--
23782384
if timeout == 0 {
2379-
return []byte{}
2385+
return b, errUSBCDCReadTimeout
23802386
}
23812387
}
23822388

@@ -2385,18 +2391,21 @@ func receiveUSBControlPacket() []byte {
23852391
for (getEPINTFLAG(0) & sam.USB_DEVICE_EPINTFLAG_TRCPT0) == 0 {
23862392
timeout--
23872393
if timeout == 0 {
2388-
return []byte{}
2394+
return b, errUSBCDCReadTimeout
23892395
}
23902396
}
23912397

23922398
// get data
23932399
bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >>
23942400
usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask)
23952401

2396-
data := make([]byte, bytesread)
2397-
copy(data, udd_ep_out_cache_buffer[0][:])
2402+
if bytesread != cdcLineInfoSize {
2403+
return b, errUSBCDCBytesRead
2404+
}
2405+
2406+
copy(b[:7], udd_ep_out_cache_buffer[0][:7])
23982407

2399-
return data
2408+
return b, nil
24002409
}
24012410

24022411
func handleEndpoint(ep uint32) {

src/machine/machine_atsamd51.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,7 @@ func handleStandardSetup(setup usbSetup) bool {
24262426
func cdcSetup(setup usbSetup) bool {
24272427
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
24282428
if setup.bRequest == usb_CDC_GET_LINE_CODING {
2429-
b := make([]byte, 7)
2429+
var b [cdcLineInfoSize]byte
24302430
b[0] = byte(usbLineInfo.dwDTERate)
24312431
b[1] = byte(usbLineInfo.dwDTERate >> 8)
24322432
b[2] = byte(usbLineInfo.dwDTERate >> 16)
@@ -2435,14 +2435,18 @@ func cdcSetup(setup usbSetup) bool {
24352435
b[5] = byte(usbLineInfo.bParityType)
24362436
b[6] = byte(usbLineInfo.bDataBits)
24372437

2438-
sendUSBPacket(0, b)
2438+
sendUSBPacket(0, b[:])
24392439
return true
24402440
}
24412441
}
24422442

24432443
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
24442444
if setup.bRequest == usb_CDC_SET_LINE_CODING {
2445-
b := receiveUSBControlPacket()
2445+
b, err := receiveUSBControlPacket()
2446+
if err != nil {
2447+
return false
2448+
}
2449+
24462450
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
24472451
usbLineInfo.bCharFormat = b[4]
24482452
usbLineInfo.bParityType = b[5]
@@ -2489,7 +2493,9 @@ func sendUSBPacket(ep uint32, data []byte) {
24892493
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos))
24902494
}
24912495

2492-
func receiveUSBControlPacket() []byte {
2496+
func receiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
2497+
var b [cdcLineInfoSize]byte
2498+
24932499
// address
24942500
usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0]))))
24952501

@@ -2504,7 +2510,7 @@ func receiveUSBControlPacket() []byte {
25042510
for (getEPSTATUS(0) & sam.USB_DEVICE_ENDPOINT_EPSTATUS_BK0RDY) == 0 {
25052511
timeout--
25062512
if timeout == 0 {
2507-
return []byte{}
2513+
return b, errUSBCDCReadTimeout
25082514
}
25092515
}
25102516

@@ -2513,18 +2519,21 @@ func receiveUSBControlPacket() []byte {
25132519
for (getEPINTFLAG(0) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 {
25142520
timeout--
25152521
if timeout == 0 {
2516-
return []byte{}
2522+
return b, errUSBCDCReadTimeout
25172523
}
25182524
}
25192525

25202526
// get data
25212527
bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >>
25222528
usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask)
25232529

2524-
data := make([]byte, bytesread)
2525-
copy(data, udd_ep_out_cache_buffer[0][:])
2530+
if bytesread != cdcLineInfoSize {
2531+
return b, errUSBCDCBytesRead
2532+
}
2533+
2534+
copy(b[:7], udd_ep_out_cache_buffer[0][:7])
25262535

2527-
return data
2536+
return b, nil
25282537
}
25292538

25302539
func handleEndpoint(ep uint32) {

src/machine/machine_nrf52840_usb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ func handleStandardSetup(setup usbSetup) bool {
445445
func cdcSetup(setup usbSetup) bool {
446446
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
447447
if setup.bRequest == usb_CDC_GET_LINE_CODING {
448-
b := make([]byte, 7)
448+
var b [cdcLineInfoSize]byte
449449
b[0] = byte(usbLineInfo.dwDTERate)
450450
b[1] = byte(usbLineInfo.dwDTERate >> 8)
451451
b[2] = byte(usbLineInfo.dwDTERate >> 16)
@@ -454,7 +454,7 @@ func cdcSetup(setup usbSetup) bool {
454454
b[5] = byte(usbLineInfo.bParityType)
455455
b[6] = byte(usbLineInfo.bDataBits)
456456

457-
sendUSBPacket(0, b)
457+
sendUSBPacket(0, b[:])
458458
return true
459459
}
460460
}

src/machine/usb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const deviceDescriptorSize = 18
1212
var (
1313
errUSBCDCBufferEmpty = errors.New("USB-CDC buffer empty")
1414
errUSBCDCWriteByteTimeout = errors.New("USB-CDC write byte timeout")
15+
errUSBCDCReadTimeout = errors.New("USB-CDC read timeout")
16+
errUSBCDCBytesRead = errors.New("USB-CDC invalid number of bytes read")
1517
)
1618

1719
// DeviceDescriptor implements the USB standard device descriptor.
@@ -405,6 +407,8 @@ type MSCDescriptor struct {
405407
out EndpointDescriptor
406408
}
407409

410+
const cdcLineInfoSize = 7
411+
408412
type cdcLineInfo struct {
409413
dwDTERate uint32
410414
bCharFormat uint8

0 commit comments

Comments
 (0)