From 5cf1b18c1e4c7d046e936a55c2d85191c67e0c77 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 15 Sep 2026 17:54:18 +0200 Subject: [PATCH] usb: terminate short control IN transfers with a ZLP AT91F_USB_SendData only took length, and every caller passed MIN(sizeof(x), wLength), so the function could not tell it had truncated. A control IN transfer ends on wLength bytes or on a short packet; returning fewer than wLength in whole 8 byte packets is neither, and the host polls until it times out. Take wLength too, do the MIN inside, and send a ZLP when the reply is shorter than asked and lands on a packet boundary. The length > 0 guard avoids a second termination, since the send loop already emits an empty packet for an empty payload. This is what the serial number descriptor's '(size % 8) == 0 OS bug workaround' padding was dodging. Not an OS bug. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 1 + common_arm/usb/usb_cdc_at91.c | 43 +++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da15e6779..dda0e9bba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed USB control transfers - a descriptor whose length is an exact multiple of the 8 byte control endpoint now ends with a zero length packet (@iceman1001) - Fixed USB WCID descriptors - the device advertised Microsoft OS descriptor support in string 0xEE but stalled the vendor request that follows (@iceman1001) - Added `hf mfdes sim` - answers GetDFNames and GetISOFileIDs, so a reader sees the ISO file ids and DF names of the simulated card (@iceman1001) - Fixed `hf 14a` - an FPGA bitstream download no longer destroys the emulator memory that `eload` just filled (@iceman1001) diff --git a/common_arm/usb/usb_cdc_at91.c b/common_arm/usb/usb_cdc_at91.c index 2479251b5..95184cbde 100644 --- a/common_arm/usb/usb_cdc_at91.c +++ b/common_arm/usb/usb_cdc_at91.c @@ -89,7 +89,7 @@ static uint8_t btConnection = 0; static uint8_t btReceiveBank = AT91C_UDP_RX_DATA_BK0; // -- pre def functions -void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length); +void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length, uint32_t wLength); void AT91F_USB_SendZlp(AT91PS_UDP pudp); void AT91F_USB_SendStall(AT91PS_UDP pudp); void AT91F_CDC_Enumerate(void); @@ -631,11 +631,22 @@ int async_usb_write_stop(void) { *---------------------------------------------------------------------------- * \fn AT91F_USB_SendData * \brief Send Data through the control endpoint + * + * length is how much the device has to give, wLength how much the host asked + * for. When less than wLength is sent and that amount is a whole number of + * control endpoint packets, the transfer needs a zero length packet to end it, + * or the host keeps polling until it times out. *---------------------------------------------------------------------------- */ -void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length) { +void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length, uint32_t wLength) { AT91_REG csr; + length = MIN(length, wLength); + + bool send_zlp = (length > 0) && + (length < wLength) && + ((length % AT91C_USB_EP_CONTROL_SIZE) == 0); + do { uint32_t cpt = MIN(length, AT91C_USB_EP_CONTROL_SIZE); length -= cpt; @@ -667,6 +678,10 @@ void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length) { UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); while (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {}; } + + if (send_zlp) { + AT91F_USB_SendZlp(pudp); + } } //*---------------------------------------------------------------------------- @@ -731,13 +746,13 @@ void AT91F_CDC_Enumerate(void) { if (bRequest == MS_VENDOR_CODE) { if (bmRequestType == MS_WCID_GET_DESCRIPTOR && wIndex == MS_EXTENDED_COMPAT_ID) { - AT91F_USB_SendData(pUdp, CompatIDFeatureDescriptor, MIN(sizeof(CompatIDFeatureDescriptor), wLength)); + AT91F_USB_SendData(pUdp, CompatIDFeatureDescriptor, sizeof(CompatIDFeatureDescriptor), wLength); return; } // wValue holds the interface number, wIndex the descriptor index. if (bmRequestType == MS_WCID_GET_FEATURE_DESCRIPTOR && wIndex == MS_EXTENDED_PROPERTIES) { - AT91F_USB_SendData(pUdp, OSprop, MIN(sizeof(OSprop), wLength)); + AT91F_USB_SendData(pUdp, OSprop, sizeof(OSprop), wLength); return; } @@ -750,16 +765,16 @@ void AT91F_CDC_Enumerate(void) { case STD_GET_DESCRIPTOR: { if (wValue == 0x100) { // Return Device Descriptor - AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength)); + AT91F_USB_SendData(pUdp, devDescriptor, sizeof(devDescriptor), wLength); } else if (wValue == 0x200) { // Return Configuration Descriptor - AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength)); + AT91F_USB_SendData(pUdp, cfgDescriptor, sizeof(cfgDescriptor), wLength); } else if ((wValue & 0xF00) == 0xF00) { // Return BOS Descriptor - AT91F_USB_SendData(pUdp, bosDescriptor, MIN(sizeof(bosDescriptor), wLength)); + AT91F_USB_SendData(pUdp, bosDescriptor, sizeof(bosDescriptor), wLength); } else if ((wValue & 0x300) == 0x300) { // Return String Descriptor const char *strDescriptor = getStringDescriptor(wValue & 0xff); if (strDescriptor != NULL) { - AT91F_USB_SendData(pUdp, strDescriptor, MIN(strDescriptor[0], wLength)); + AT91F_USB_SendData(pUdp, strDescriptor, strDescriptor[0], wLength); } else { AT91F_USB_SendStall(pUdp); } @@ -799,25 +814,25 @@ void AT91F_CDC_Enumerate(void) { pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN) : 0; break; case STD_GET_CONFIGURATION: - AT91F_USB_SendData(pUdp, (char *) & (btConfiguration), sizeof(btConfiguration)); + AT91F_USB_SendData(pUdp, (char *) & (btConfiguration), sizeof(btConfiguration), wLength); break; case STD_GET_STATUS_ZERO: wStatus = 0; // Device is Bus powered, remote wakeup disabled - AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus)); + AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus), wLength); break; case STD_GET_STATUS_INTERFACE: wStatus = 0; // reserved for future use - AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus)); + AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus), wLength); break; case STD_GET_STATUS_ENDPOINT: wStatus = 0; wIndex &= 0x0F; if ((pUdp->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= AT91C_EP_NOTIFY)) { wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1; - AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus)); + AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus), wLength); } else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == AT91C_EP_CONTROL)) { wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1; - AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus)); + AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus), wLength); } else { AT91F_USB_SendStall(pUdp); } @@ -875,7 +890,7 @@ void AT91F_CDC_Enumerate(void) { break; } case GET_LINE_CODING: - AT91F_USB_SendData(pUdp, (char *) &line, MIN(sizeof(line), wLength)); + AT91F_USB_SendData(pUdp, (char *) &line, sizeof(line), wLength); break; case SET_CONTROL_LINE_STATE: btConnection = wValue;