Skip to content

Commit

Permalink
Merge pull request #315 from particle-iot/fix/p2-usb-driver-enum
Browse files Browse the repository at this point in the history
[rtl872x] hal: avoid initializing USB driver twice which causes enumeration issues
  • Loading branch information
avtolstoy authored Dec 30, 2021
2 parents b98185d + 979f450 commit c138471
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 6 additions & 4 deletions hal/src/rtl872x/usbd_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,13 @@ int Device::clearConfig(unsigned index) {
int lastError = 0;
for (auto& cls: classDrivers_) {
if (cls && cls->isEnabled()) {
int r = cls->deinit(index);
if (r < 0) {
lastError = r;
if (cls->isConfigured()) {
int r = cls->deinit(index);
if (r < 0) {
lastError = r;
}
cls->configured(false);
}
cls->configured(false);
}
}
return lastError;
Expand Down
17 changes: 15 additions & 2 deletions hal/src/rtl872x/usbd_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,28 @@ RtlUsbDriver::~RtlUsbDriver() {
}

int RtlUsbDriver::attach() {
if (initialized_) {
// Ignore
return 0;
}
initialized_ = true;
// NOTE: these calls may fail
usbd_init(&usbdCfg_);
usbd_register_class(&classDrv_);
auto r = usbd_init(&usbdCfg_);
if (r) {
// LOG(ERROR, "usbd_init failed: %d", r);
initialized_ = false;
CHECK_RTL_USB_TO_SYSTEM(r);
} else {
usbd_register_class(&classDrv_);
}

return 0;
}

int RtlUsbDriver::detach() {
usbd_unregister_class();
usbd_deinit();
initialized_ = true;
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions hal/src/rtl872x/usbd_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class RtlUsbDriver : public DeviceDriver {
#if MODULE_FUNCTION != MOD_FUNC_BOOTLOADER
RecursiveMutex mutex_;
#endif // MODULE_FUNCTION != MOD_FUNC_BOOTLOADER

volatile bool initialized_ = false;
};

} // namespace usbd
Expand Down

0 comments on commit c138471

Please sign in to comment.