Skip to content

Commit dba59a0

Browse files
author
Alrik Vidstrom
committed
Fix crash bugs on disconnect
Adds a nullptr check in the error recovery code that prevents crashes on disconnect. Also prevents the use of an uninitialized pointer in the same code by tweaking the connect bug fix from the previous commit.
1 parent 761ddb9 commit dba59a0

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/USBHost/USBHost.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,16 +697,8 @@ bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint
697697
default:
698698
return false;
699699
}
700-
#else
701-
// The code that follows this check shouldn't be executed for a control endpoint
702-
if (CONTROL_ENDPOINT == (ep->getType()))
703-
{
704-
return true;
705-
}
706700
#endif
707701
ep->dev = dev;
708-
// This check might not be necessary when the control endpoint check is added above, but
709-
// is included just in case
710702
if (nullptr != dev)
711703
{
712704
dev->addEndpoint(intf_nb, ep);

src/targets/TARGET_STM/USBEndpoint_STM.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ void USBEndpoint::setState(USB_TYPE st)
146146
/* small speed device with hub not supported
147147
if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
148148

149-
// Notice that dev->getAddress() must be used instead of device_address, because the latter will contain
150-
// incorrect values in certain cases
151-
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, dev->getAddress(), hcd_speed, type, size);
152-
// HAL_HCD_HC_Init() doesn't fully enable the channel after disable above, so we do it here -->
153-
USBx_HC(hced->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
154-
USBx_HC(hced->ch_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
155-
// <--
149+
// If the device seems to be gone, there is no use trying to enable the channel again
150+
if (nullptr != dev)
151+
{
152+
// Notice that dev->getAddress() must be used instead of device_address, because the latter will contain
153+
// incorrect values in certain cases
154+
HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, dev->getAddress(), hcd_speed, type, size);
155+
// HAL_HCD_HC_Init() doesn't fully enable the channel after disable above, so we do it here -->
156+
USBx_HC(hced->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHDIS;
157+
USBx_HC(hced->ch_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
158+
// <--
159+
}
156160
}
157161
}
158162

0 commit comments

Comments
 (0)