From 39e033366311d0222f3ce11deea517d45cf89919 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 4 Dec 2025 18:05:37 -0500 Subject: [PATCH] fix: don't hardcode success return state fix: validate bmRequestType\nfix: mimic tinyusb behavior in mscStateNeedReset\nfix: CLEAR_FEATURE requests to iface addr are against spec --- src/machine/usb/msc/setup.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/machine/usb/msc/setup.go b/src/machine/usb/msc/setup.go index 3d5bef2d5..00507aac6 100644 --- a/src/machine/usb/msc/setup.go +++ b/src/machine/usb/msc/setup.go @@ -3,7 +3,6 @@ package msc import ( "machine" "machine/usb" - "machine/usb/msc/csw" ) func setupPacketHandler(setup usb.Setup) bool { @@ -18,11 +17,17 @@ func (m *msc) setupPacketHandler(setup usb.Setup) bool { wValue := (uint16(setup.WValueH) << 8) | uint16(setup.WValueL) switch setup.BRequest { case usb.CLEAR_FEATURE: - ok = m.handleClearFeature(setup, wValue) + if setup.BmRequestType == 0x02 { // Host-to-Device | Standard | Endpoint + ok = m.handleClearFeature(setup, wValue) + } case usb.GET_MAX_LUN: - ok = m.handleGetMaxLun(setup, wValue) + if setup.BmRequestType == 0xA1 { // Device-to-Host | Class | Interface + ok = m.handleGetMaxLun(setup, wValue) + } case usb.MSC_RESET: - ok = m.handleReset(setup, wValue) + if setup.BmRequestType == 0x21 { // Host-to-Device | Class | Interface + ok = m.handleReset(setup, wValue) + } } return ok } @@ -53,24 +58,25 @@ func (m *msc) handleClearFeature(setup usb.Setup, wValue uint16) bool { } else if wIndex == usb.MSC_ENDPOINT_OUT { m.stallEndpoint(usb.MSC_ENDPOINT_OUT) } - return ok + machine.SendZlp() + return true } // Clear the direction bit from the endpoint address for comparison wIndex := setup.WIndex & 0x7F - // Clear the IN/OUT stalls if addressed to the endpoint, or both if addressed to the interface - if wIndex == usb.MSC_ENDPOINT_IN || wIndex == mscInterface { + // Clear the IN/OUT stalls if addressed to the endpoint + if wIndex == usb.MSC_ENDPOINT_IN { m.clearStallEndpoint(usb.MSC_ENDPOINT_IN) ok = true } - if wIndex == usb.MSC_ENDPOINT_OUT || wIndex == mscInterface { + if wIndex == usb.MSC_ENDPOINT_OUT { m.clearStallEndpoint(usb.MSC_ENDPOINT_OUT) ok = true } // Send a CSW if needed to resume after the IN endpoint stall is cleared if m.state == mscStateStatus && wIndex == usb.MSC_ENDPOINT_IN { - m.sendCSW(csw.StatusPassed) + m.sendCSW(m.respStatus) ok = true }